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>
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'];
I need help with my form. There's a mix of input, textarea and file upload that i want to enter into the database..
What do I use in the ? Do I use the normal form attribute :
<form action="" method="">
or
<form enctype="" action="" method="">
Please have it in mind that, I have to do this in a single page, and the picture upload must be done along with other text input.
Thanks for your time.
You must use enctype="multipart/form-data" for file uploading, this will also work fine for non-file upload forms.
You need to set enctype="multipart/form-data" and use method="post" for any form that includes a file input. This won't stop you from including other types of fields.
(The way those fields will be submitted to the server will change, but your form parsing library will deal with the differences automatically, you only need to worry about them if you are parsing the raw input yourself).
<form enctype="multipart/form-data" method="post" action="submit.php">
submit.php being, in this case, the external PHP script that will process your form ( if you decide to use PHP ). But you can name the .php script whatever you like ( e.g. cats.php ).
The uploaded file/image data will be stored inside $_FILES, and all the textfield, textarea, radio buttons, check boxes and other data will reside inside the $_POST superglobal.
When submit.php receives the submitted form you can do all kinds of processing on it such as validating that user has submitted the correct type of file/image, store the file path of the file/image in your local database( client/server or file system based ), and much more.
Make sure to validate user input client side and server side too.
<form enctype="multipart/form-data" action="yourpage.php" method="post">
You'll need the enctype attribute if you want the file upload to work. FYI, a form can contain every field type, including file uploads, and work just fine.
In Classic ASP I had to access my textfield as load.getFileData("textfield")
instead of the standard Request("textfield") when using the enctype="multipart/form-data"
I have a form where I'm posting different fields and every type of field posted seems to work except the input File type.
I'm using var_dump($_POST); and all the other fields are there but nothing in the input type file.
My form part looks like this:
<form enctype="multipart/form-data" id="ajax-form" action="index2.php" method="POST" data-ajax="true">
and works well for everything else.
If there anything that's different in the input type file?
<input type="text" id="myid" name="myid" value="" /> ..This posts value
<input id="theimage" name="theimage" type="file" /> .. does not post value
Any ideas anyone?
Files are stored in $_FILES, not $_POST
http://php.net/manual/en/reserved.variables.files.php $_FILES variable
http://www.php.net/manual/en/features.file-upload.php Manual on PHP File Uploads.
To handle the file (no error checking):
$ROOT = "/path/to/store/files";
foreach($_FILES as $file => $details)
{ // Move each file from its temp directory to $ROOT
$temp = $details['tmp_name'];
$target = $details['name'];
move_uploaded_file($temp, $ROOT.'/'.$target);
}
See also http://www.php.net/manual/en/function.move-uploaded-file.php for more examples.
Actually if you try Firefox (>13), you can get the posted value. But if you are using Chrome or Safari, you can't get the posted value. I think the it is related with the Browsers.
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
My Code :
<?php
function dbAdd($first_name , $image) {
//mysql connect database code...
mysql_query("INSERT INTO users SET first_name = '".$first_name."', image = '".$image."'");
$mysql_close($sql);
}
if($_SERVER['REQUEST_METHOD']=='POST') {
dbAdd($_POST['first_name'], $_POST['image']);
}
?>
<form enctype="multipart/form-data" method="post" action="">
First Name : <input type="text" name="first_name" >
Image : <input type="file" name="image">
<input type="submit">
</form>
The form "file" is to upload. I know that. But I wonder how to get the values so I can put the path of image in the database. The code is already working. The $first_name can already save to the database.
Thank you for the answers.
Jordan Pagaduan
The file will be uploaded to a temporary place on the server when the form is submitted.
Once the form has been submitted, the $_FILES variable will contain all the files submitted. In your case, you could access the uploaded file using $_FILES['image']. Most likely you will want to move the file out of the temporary directory to a safer place.
For more info, have a look at the PHP manual on the topic, specifically the page on handling POST uploads. That second page has an example for you on how to move the uploaded file (have a look at the move_uploaded_file() method).
Straight from W3C: Upload form & $_FILE variable