Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Posting input type file problem No Value posted

Tags:

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?

like image 713
Satch3000 Avatar asked Aug 16 '11 23:08

Satch3000


People also ask

How do you give a value to an input type file?

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.

How do you check if file is uploaded or not in JavaScript?

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.

How do you attach a file in HTML?

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!

How do you accept an attribute in a file upload?

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.


1 Answers

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.

like image 93
David Souther Avatar answered Oct 02 '22 16:10

David Souther