Solved

$_FILES not set?

Posted on 2012-03-12
6
302 Views
Last Modified: 2012-03-15
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
Comment
Question by:pillmill
  • 3
  • 2
6 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 37712085
No, you cannot force your client to upload files.  You cannot prepopulate the $_FILES array.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 37712205
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
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 37712209
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 108

Expert Comment

by:Ray Paseur
ID: 37712210
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
 

Author Comment

by:pillmill
ID: 37712548
Thanks. The <form> tag looks like:

<form name="txt" method="post" enctype="multipart/form-data" action="/dir1/dir2/" onSubmit="return check()">
0
 
LVL 34

Accepted Solution

by:
gr8gonzo earned 500 total points
ID: 37720103
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
An enjoyable and seamless user experience can go a long way on an eCommerce site. While a cohesive layout and engaging copy play roles in creating a positive user experience, some sites neglect aspects that seem marginal but in actuality prove very …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now