When I echo $_FILES['file']['name'] it gives it returns nothing
When I echo $_FILES['file']['name'][0] it returns the correct data.
How come that in every example they can use $_FILES['file']['name'] directly without the extra index?
How the $_FILES array is constructed is based on how your html input tags are named.
Your html input is likely
<input type="file" name="file[]" />
while in most examples it is
<input type="file" name="file" />
The first solution allows to duplicate this line exactly to receive multiple files simultaneously (in an array):
<input type="file" name="file[]" />
<input type="file" name="file[]" />
The second solution only allows exactly this single one file. If one needs to support multiple fileuploads, one has to create a html tag with a different value for the name attribute:
<input type="file" name="file1" />
<input type="file" name="file2" />
See this user comment in the php manual for a detailed example and the other comments around for futher information how to reorder this array (if one wishes to do so for whatever reason...)
Related
I would like to have an upload form that automatically submits as soon as the user has selected a file.
This question has been asked many times before. If I understood the other threads correctly, this should work:
<form method="post" enctype="multipart/form-data">
<input id="fileToUpload" onchange="form.submit()" type="file"/>
</form>
<?php
if(isset($_FILES["fileToUpload"])){
echo "You successfully entered a file for the upload!";
// move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], "uploads/my_image.png");
}
?>
When I select a file, I see the file name briefling flashing instead of the "no file chosen". But $_FILES is not set.
Replacing onchange="form.submit()" with onchange="alert('test');" leads to the expected alert box after file selection. (idea from here: https://stackoverflow.com/a/1904189/11826257)
I also tried it with an extra JavaScript function as described here: https://stackoverflow.com/a/12275917/11826257. That didn't work neither.
Do modern browsers block onchange="form.submit()" for type="file"?
I tried it with Firefox 68 and Microsoft Edge 84.
To allow $_FILES to detect your uploaded file, you need name attribute in your input tag. So change
<input id="fileToUpload" onchange="form.submit()" type="file"/>
to
<input id="fileToUpload" name="fileToUpload" onchange="form.submit()" type="file"/>
Form id values are not submitted by default. Only name values are. Try changing id to name in the input tag, e.g.:
<input name="fileToUpload" onchange="form.submit()" type="file"/>
My question, it´s very short.
is it possible to detect the kind of types we send as POST from a form?
If I have some fields and one field for each input type as text, the other textarea, the other file, etc, is it possible to use Php to detect, in each case, the type from these fields.
<form method="post">
<input type="text" name="test1" value="ok">
<textarea name="text2"></textarea>
<input type="file" name="test3" value="ok">
</form>
And finally determine whether the type is text, textarea, file, etc
I know it´s possible with Jquery buy i don´t find nothing about this with php.
Thank´s for the help in advanced
No, request contains only key-value pairs. But you can add additional hidden fields that passes these data. i.e. (for larger forms I's suggest it automaticaly with JS):
<input type="hidden" name="text2_type" value="textarea">
<input type="hidden" name="test3_type" value="file">
or
<input type="hidden" name="types[text2]" value="textarea">
<input type="hidden" name="types[test3]" value="file">
Other solution
Also you can just introduce naming convention for your fields, i.e.:
<textarea name="text2_textarea"></textarea>
or
<textarea name="textarea[text2]"></textarea>
so in your PHP you can check if key ends with _texteraea or is in array $_POST['textarea'] to determine the type o field.
I have a very basic PHP multiple file upload form:
<form action="file.upload.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="id" value="<?php echo $_REQUEST['id']; ?>" />
<input type="hidden" name="bypass" value="1" />
<input multiple type="file" name="file[]" id="file">
<input type="submit" name="submit" value="Submit">
</form>
Everything works fine for a few files, but when I upload too many large files, the hidden variables are no longer posted to PHP (!)
I am not sure what is causing the from variables to be ignored by PHP (?)
(I've checked with fiddler and the form indeed sends the right data from clientside... i.e., form fields bypass and id are both properly populated on clientside...)
Is it possible with codeigniter, or even at all, to have only one input field that allows a user to select multiple files to upload. So basically you have
<input name="files" type="file">
rather than
<input name="file1" type="file">
<input name="file2" type="file">
<input name="file3" type="file">
I currently know how to implement the latter but think the former would be a cleaner model.
Have you even looked into PHP manual?
<form action="file-upload.php" method="post" enctype="multipart/form-data">
Send these files:<br />
<input name="userfile[]" type="file" multiple=""/><br />
<input type="submit" value="Send files" />
</form>
you access it in PHP as $_FILES
Your input has to be like this (so, you can select multiple files at once)
<input multiple type="file" name="files[]" />
Notice multiple attribute but not supported by all/old browsers.
An Example here and also take look at this tutorial.
I have a HTML form page with following code :
<form action="chainresult.php" method="post" enctype="multipart/form-data" />
<input type="hidden" name="MAX_FILE_SIZE" value="3145728"/>
<input type="file" name="userfile" id="userfile" size="30" />
<input type="submit" value="GET SEQUENCE" />
</form>
<form action="helix_info.php" method="post" enctype="multipart/form-data" />
<input type="hidden" name="MAX_FILE_SIZE" value="3145728"/>
<input type="file" name="userfile" id="userfile" size="30" />
<input type="submit" value="GET HELIX INFO" />
</form>
My page has two browse options and two submit options which takes the use to 2 php pages. I want to have only one browse option with two options that takes the user to 2 different php pages based on what the user clicks.
Any help is appreciated!
You will need to combine the two forms into one (you don't even necessarily need the form tags), use JavaScript or jQuery to capture the submit button click, evaluate the input value based on your validation rules that route the form submission, and then post the values to a form, likely through ajax.
You can submit multiple forms but you will have to use Javascript. It should be doable with jQuery without too much sweat and tears. Something like...
$("#my-submit-button").click(function(){
$("#first-form").submit();
$("#second-form").submit();
})
I am not sure I understand your question correctly, but if you want to be able to post data to two different URLs, with two different submit-buttons, having two different forms is the only way to do it with plain HTML.
However, it would be possible to use JavaScript. In that case you could mash both forms together, evaluate the input before sending any information, and the post the data to different URLs depending on the input, using AJAX.
It is worth noting that going down the JavaScript-road, you would make the form unusable for anyone who has disable JavaScript.
I would probably suggest that you make it a single form, point it to an URL that can handle either case. So you always post the data to the same URL, and the server-side code would have to evaluate the input and decide what to do with it. That case you don't eliminate users that doesn't have JavaScript activated.
Not sure if I understood the question properly, but you could use jQuery to change the action attribute of your form, depending on what the user chooses. Something among the lines of:
<form id="myform" action="dummy.php" method="post" enctype="multipart/form-data" />
<input type="hidden" name="MAX_FILE_SIZE" value="3145728"/>
<input type="file" name="userfile" id="userfile" size="30" />
<input type="radio" name="formtype" value="uploadscript1.php" /> Option 1<br>
<input type="radio" name="formtype" value="uploadscript2.php" /> Option 2<br>
<input type="submit" value="GET HELIX INFO" />
</form>
And in jQuery:
$('input[name="formtype"]').change(function(){
$('#myform').attr('action', $(this).attr('value'));
});
I am not sure about the jQuery part, but it should work ok. Try experimenting with that. :)
Using this approach you should be able to send the same data to two different forms.