I have a form where a user submits the description of an object (including an image) and there is JavaScript that adds an additional set of inputs for +1 object description. When the form is submitted the file information is not stored in $_FILES.
The form tag is <form id="order_form" name="order_form" method="post" action="#">
The file type input is <input type="file" name="order_image[]" />.
print_r($_FILES); returns
array()
Does anyone have an idea as to why this might be happening. I will gladly include any other information that might be pertinent to this question.
Have you set the correct encoding type in your form tag?
<form enctype="multipart/form-data" method="post" action=...>
<form enctype="multipart/form-data" method="POST">
be sure you include enctype
Related
I am trying to create a simple PHP Contact form using PHPMailer (because I also want to attach files through the form). And somehow something that should be really simple managed to give me headaches.
Here are some lines of code:
<form method="POST" action="" enctype= multipart/form-data">
.....
<input type="file" name="file">
</form>
.....
$file = $_POST['file']['tmp_name'];
echo $file;
My main problem is that I attach a file, complete all the fields, submit the form. I receive the email except the file attached. I tracked down and found out that, if I echo the $file var, It will display the first letter of the file.
Ex: if the file is named test.jpg, echoing $file will result in a t.
I have no idea what is this happening, judging the fact that there aren't too many lines of code and nothing that will change the filename..
Hope someone can help me out.
" missing in enctype and use $_FILES instead of $_POST
<form method="POST" action="" enctype= "multipart/form-data">
.....
<input type="file" name="file">
</form>
AND
$file = $_FILES['file']['tmp_name'];
<?php
var_dump($_POST);
var_dump($_FILES);
?>
<!doctype html>
<form id="register_form" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="4194304" />
<input type="file" name="file_upload" accept="image/*" /><br>
<input type="submit" value="Upload" id="submit_button">
</form>
When uploading a file, for example, tipsfedora.png, I get the file in the $_POST array, yet no files in the $_FILES array. Any help would be appreciated. Full code is posted. I would like to use the files with bulletProof image upload, yet the problem is that $_FILES is an empty array.
$_FILES contains content of uploaded files. But you actually didn't upload any files because the form doesn't have enctype attribute. It is required to enable file uploading.
But even you didn't upload any file, $_POST contains the file name.
Solution? Just add to enctype="multipart/form-data" to the <form>.
The MDN document about enctype may help you.
You forgot to add
enctype="multipart/form-data"
to the <form> tag
This is one of the most bizarre bugs I've encountered. I have an HTML form that I use for uploading a file and also sending some additional hidden inputs, which gets processed in a controller method, like bellow.
Just fyi, whether I use the CI form helper functions or write raw HTML code, the output HTML is as follows:
<form action="http://mysite/dev/insert-images" id="imageSelectionForm" enctype="multipart/form-data" method="post" accept-charset="utf-8">
<input type="file" name="userimage">
<input type="hidden" name"something"="" value="somevalue">
<button type="submit">Add image(s)</button>
</form>
The problem is that when I remove the enctype from the form, I see the POST-ed data on the "other side". All of it. Otherwise, meaning if I do use the 'multipart' encryption with the form, the POST array appears empty.
public function insertImages() {
echo "in the method";
print_r($_POST);
}
What is happening exactly?
By now I know to upload file form is using ENCTYPE="multipart/form-data. But when we want to transfer form datat to mysql via php file what should be the enctpe=""?
<form action="process.php" method="POST" name="contactform" onSubmit="return ValidateForm(this); ENCTYPE="multipart/form-data">
Can we use ENCTYPE="multipart/form-data" if not what should i use???
Please answer me
Thanks
You don't need to fill in a form enctype when you aren't using a file upload.
The default value is: "application/x-www-form-urlencoded". (when enctype not provided)
Conclusion: leave empty or use "multipart/form-data" when you need to upload a file.
See: http://www.w3schools.com/tags/att_form_enctype.asp
Basically ENCTYPE is the property of form. This is not necessory to use this property, it is optional.
I have a question about the $_FILES global variable in php. When I do a print_r on my files array I get a blank array() 1 displayed (but I have uploaded one file with post from a form on another page). The post variable for upload (upload being the input type = file name) is set to the filename but nothing in Files is set and if I try and call $_FILES['upload']['name'] nothing shows up. What could be causing this? When I submit my form I have a bunch of different fields (text boxes, select boxes, checkboxes, etc.) but that shouldn't effect my file upload right?
Thanks!
Make sure your form tag has the enctype=multipart/form-data attribute set:
<form action="test.php" method="POST" enctype="multipart/form-data">
You need to make sure your form has the enctype attribute, e.g.:
<form method="post" enctype="multipart/form-data">
<input type="file" name="fileUpload"/>
<input type="submit"/>
</form>