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?
The only way to set the value of a file input is by the user to select a file. This is done for security reasons. Otherwise you would be able to create a JavaScript that automatically uploads a specific file from the client's computer.
length property in jQuery to check the file is selected or not. If element. files. length property returns 0 then the file is not selected otherwise file is selected.
The <input type="file"> defines a file-select field and a "Browse" button for file uploads. To define a file-select field that allows multiple files to be selected, add the multiple attribute. Tip: Always add the <label> tag for best accessibility practices!
The accept attribute specifies a filter for what file types the user can pick from the file input dialog box. Note: The accept attribute can only be used with <input type="file"> . Tip: Do not use this attribute as a validation tool. File uploads should be validated on the server.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With