• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 323
  • Last Modified:

$_FILES not set?

In debugging some legacy code, I found that the $_FILES values are not set when
an input type=file is activated on the client.

The client code is:
     
<input type="file" ...

But the server shows a negative result when the test is made of:

    if(!empty($_FILES))

How can I fix or debug this? Is there a way to force these actions?
0
pillmill
Asked:
pillmill
  • 3
  • 2
1 Solution
 
Ray PaseurCommented:
No, you cannot force your client to upload files.  You cannot prepopulate the $_FILES array.
0
 
Ray PaseurCommented:
This may help clarify how to handle $_FILES.  See
http://www.laprbass.com/RAY_FILES.php
<?php
error_reporting(E_ALL);

// IS ANYTHING THERE?
if ( (!empty($_POST)) || (!empty($_FILES)) )
{
   echo "<pre>";
   echo "FILES: ";
   var_dump($_FILES);
   echo PHP_EOL . PHP_EOL;
   echo "POST: ";
   var_dump($_POST);

   // TEST TO SEE IF A FILE WAS UPLOADED
   if (empty($_FILES)) echo PHP_EOL . '$_FILES IS EMPTY()';
   if ($_FILES["myFile"]["error"] == 4) echo PHP_EOL . 'NO FILE WAS UPLOADED';
   echo "</pre>\n";
}
?>
<form enctype="multipart/form-data" method="post" name="myForm">
<input type="file" name="myFile" onChange="javascript:alert('foo');" />
<input type="hidden" name="myHidden" value="X" />
<input type="submit" />
</form>

Open in new window

This shows the error codes for file uploads.
// LIST OF THE ERRORS THAT MAY BE REPORTED IN $_FILES[]["error"] (THERE IS NO #5)
$errors = array
( 0 => "Success!"
, 1 => "The uploaded file exceeds the upload_max_filesize directive in php.ini"
, 2 => "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form"
, 3 => "The uploaded file was only partially uploaded"
, 4 => "No file was uploaded"
, 5 => "UNDEFINED ERROR"
, 6 => "Missing a temporary folder"
, 7 => "Cannot write file to disk"
)
;

Open in new window

0
 
gr8gonzoConsultantCommented:
Are you saying that when a client uploads a file with <input type="file"> (they select a file, then click on a submit button, wait for the upload to finish, and the $_FILES is not populated in the resulting page) ?

If so, what does your <form> tag look like on the client side?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Ray PaseurCommented:
Please see line 1 of the first code snippet in the previous post.  This is an artifact of the EE "upgrade" that we are still dealing with.  It happens if you put a code snippet after a URL.  Sorry about that.  Maybe they will fix it someday.
0
 
pillmillAuthor Commented:
Thanks. The <form> tag looks like:

<form name="txt" method="post" enctype="multipart/form-data" action="/dir1/dir2/" onSubmit="return check()">
0
 
gr8gonzoConsultantCommented:
So, $_FILES will ONLY be available AFTER the form submits. So your action is currently "/dir1/dir2/", which probably means that there is an index.php or something insider "dir2" that is receiving the form post. Once you hit submit on the form, THAT is when the form receiver will be able to see $_FILES. Does that make sense?
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now