I have a Form with some fields:
<form action="xyz.php" method="post" enctype="multipart/form-data">
<input type="text" placeholder="First name" name="firstname">
...
<input type="file" name="logo">
...
<input type="submit" name="submit">
</form>
The image should be uploaded immediately to img_upload.php. In this file i need the $_FILES array.
My img_upload.php script uploads the image, do some things with the image (resize, ...) and give me the URL to the image. After this, the image should be displayed in the form.
Is there any chance to upload the image (send $_FILES array to another file) without submiting the whole form?
<input type="file" name="logo" onchange="uploadThisFile(this)" >
function uploadThisFile(file) {
/* launch an ajax request to img_upload.php and pass file details via POST */
}
Related
My question is straightforward, i want my Form to be just an input button to browse for the file and do the work too instead of two inputs like in the image bellow :
here is my code :
**EDIT : ** all I want is ONE BUTTON not two like in the picture , one button to do the work preferably called "upload image"
in the image bellow the form consists of two buttons one does the choosing and another one does the submitting of the form action.
<form class='formUpload' action="uploadImage.php" method="POST" enctype="multipart/form-data">
<input value="Upload Image" class="ToUpload" name="fileToUpload" type="file" />
<input type="submit" name="fileToUpload" value="Upload Image">
</form>
ok let me add an example asumming you are expecting the same here is something that you might be looking:
HTML:
<form method="post" action="abc.php" name="imageform" id="imageform">
Choose File :<input type="file" name="file" id='fileupload'>
</form>
js:
$(document).ready(function(){
$('#fileupload').on('change',function(){
$('#imageform').submit();
});
});
I searched internet and I could not find a solution for the problem. I have a form and when I submit only text fields there is no problem. But when I add file input and submit the form I get undefined index error.
HTML CODE
<form method="post" action="add.php" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="10485760">
<input type="text" name="topic" style="width:300px;" value="cars" />
<input type="file" name="file1" id="file1" />
<input type="submit" name="submit" value="Add"/>
</form>
PHP CODE
if(isset($_POST['submit']))
{
// Form
}
else echo'NOOO';
This code always give NOOO when uploading file. I have controlled php.ini and upload is on.
Not every Browser sends the Submit button Value
you should check for your real Input values
if(isset($_POST['topic']))
{
// Form
}
else echo'NOOO';
if you want to check if your File is attched
use
$_FILE['file1']
instead of $_POST
I was wondering whether it is possible to rename an image base on the form input file ID.
<form action="upload_file.php" enctype="multipart/form-data" method="post">
<input id="picture_01" type="file">
<input id="picture_02" type="file">
<input id="picture_03" type="file">
<input id="picture_04" type="file">
<input name="submit" type="submit" value="Submit">
</form>
I want that if the image is uploaded from input 4 it will be renamed to 'picture_04', if it is from input form 2 it will be renamed to 'picture_02'. Not sequencially but according to the input form box.
I haven't managed to do this despite the various trial and errors.
I would use separate forms for each input. This way you could use a hidden input like:
<form action="upload_file.php" enctype="multipart/form-data" method="post">
<input type='hidden' name='picture_03_file' value="picture_03" />
<input type='file' name='picture_03_name' />
</form>
<form action="upload_file.php" enctype="multipart/form-data" method="post">
<input type='hidden' name='picture_04_file' />
<input type='file' name='picture_04_name' value="picture_04" />
</form>
This way your PHP code would look like:
$imgName = $_POST['picture_04_name'];
// Do file upload here
You need to name your inputs:
<input id="picture_01" name="picture_01" type="file">
etc.
Then in PHP you retrieve the image with the $_FILES array, like $_FILES['picture_01'] or by simply looping through $_FILES.
foreach( $_FILES as $input_name=>$file)
{
// $input_name is the name used as the form input name
// $file is an array with the following keys: name, type, tmp_name, error, size.
}
Of course the manual is always a good read http://www.php.net/manual/en/features.file-upload.post-method.php
I have a simple photo gallery using CodeIgniter that displays thumbnails and select buttons for each image.
On this page I also have a choose file and 'upload' button and a 'delete selected items' button.
<form action="http://localhost:8080/PhpProject1/gallery"
method="post" accept-charset="utf-8" enctype="multipart/form-data">
<input type="file" name="userfile" value="">
<input type="submit" name="upload" value="Upload">
<input type="submit" name="delete" value="Delete Selected">
</form>
My check boxes are grouped using the following style (i.e. 'photos[]' for group):
<input type="checkbox" name="photos[]" value="IMG_20120709_151023.jpg">
When debugging with Netbeans I am definitely calling the right method by getting the name value from the post data but with the 'delete' method the post data contains nothing else, just the input name and value (key= delete, value = Delete Selected)using.
Here is the php code:
$this->load->model('gallery_model');
if ($this->input->post('upload')) {
$this->gallery_model->do_upload($order_no);
redirect('gallery');
}
if ($this->input->post('delete')) {
$this->gallery_model->do_delete($order_no); // this is getting called ok, just no $_post data??
redirect('gallery');
}
Is there something else I need to do to ensure the post request is picking up the selected items?
I'd like to do this with php but if I have to go down the ajax route so be it, thanks.
Mick.
I'm struggling on letting the user upload and image to an image field on the HTML doc. When i test it, i can select which image to upload and then click "UPLOAD" but it doesn't go anywhere or go to the field i want to designate it to.
<input name="imgfield" type="image" width="100" height="100">
<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
<p>
<label for="file">Select Picture</label>
<input type="file" name="file" id="file" />
<input type="submit" name="btn_uploadpic" id="btn_uploadpic" value="UPLOAD"
<?php
if (isset($_POST['btn_uplaodpic']))
{
$id=$_POST['imgfield'];
}
?>/>
</p>
</form>
When uploading a file, you won't get the uploaded file in $_POST, but in $_FILES. Check here for a quite detailed example on how to upload files with PHP:
http://www.w3schools.com/php/php_file_upload.asp
You have to refer to a temporary filename, using files, you can do something like
$_FILES['imgfield']['tmp_name']; // temp_name can be anything
If referring to 'name' isn't enough then give the input an 'id' of value 'imgfield'