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
Related
Form 1:
<?php
echo $this->upload_message;
?>
<form enctype="multipart/form-data" method="post" name="mfuploaderwp-uploadform" action="?uploadfile">
Upload file: <input name="mfuploadwp-filename" type="file">
<input class="mfuploadwp-submit" type="submit" value="Upload" name="submit"></form>
Form 2:
<?php
echo $this->upload_message;
?>
<form enctype="multipart/form-data" method="post" name="mfuploaderwp-uploadform" action="?uploadfile">
Upload file: <input name="mfuploadwp-filename" type="file">
<input class="mfuploadwp-submit" type="submit" value="Upload" name="submit"></form>
Is it possible to fetch which form is submitted without giving any extra attributes or so to above forms? The forms are created dynamically based on what user enter for amount number of forms. (In this case user has entered 2 forms)
I want to do this so $this->upload_message would be accurate only for the form that is used for uploading.
Alter the name tags on your <input type="submit"> buttons. Have one as name="submit" and the other as name="submit_two" (for example, bad naming convention), then process code as
if (isset($_POST['submit'])) {
// do stuff
} elseif (isset($_POST['submit_two'])) {
// do other stuff
}
Yes, it's possible.
The cleanest way, in my opinion, is to put an hidden input tag in each form:
<form enctype="multipart/form-data" method="post" name="mfuploaderwp-uploadform" action="?uploadfile">
<input type="hidden" name="active_form" value="1">
(...)
and
<form enctype="multipart/form-data" method="post" name="mfuploaderwp-uploadform" action="?uploadfile">
<input type="hidden" name="active_form" value="2">
(...)
then, in the page that process the form, you can check it in this way:
if( $_POST['active_form'] == 1)
{
(...)
}
elseif( $_POST['active_form'] == 2)
{
(...)
}
If your form is generated dynamically based on the user input(The forms are created dynamically based on what user enter for amount number of forms), in this case you can use three type of solution as far as I know,
You can introduce a new hidden field for each form based on the form number.
Eg:
Upload file: <input name="mfuploadwp-filename" type="file">
<input class="mfuploadwp-submit" type="submit" value="Upload" name="submit">
<input type="hidden" value="1" name="form_id"/>
</form>
in php
switch($_POST['form_id']) {
//the form data to be processed..
}
or
You can update the input field submit button naming based on the form number.
Eg:
Upload file: <input name="mfuploadwp-filename" type="file">
<input class="mfuploadwp-submit" type="submit" value="Upload" name="submit_{form_id}">
you can add a additional parameter in the form method.
...
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 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 */
}
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'
I am trying to upload a file using php,and i need to pass a value like id or some thing with the from the upload form page to the file that have php code plz look at this code:
<form name = "file" enctype="multipart/form-data" action="uploadpp.php" method="POST">
Please choose a file: <input name="uploaded" type="file" /><br />
<input type=button name="Submit" value="Submit" />
</form>
can i send the value with the same post array??
thanks in advance.
You can use hidden form fields:
<input type="hidden" name="foo" value="bar" />
So in PHP, $_POST['foo'] would give you "bar"
Alternatively you can add them as "GET parameters" into the form's action attribute:
<form ... action="uploadpp.php?foo=bar" ...>
And access that with $_GET['foo']