Solved

$_FILES not set?

Posted on 2012-03-12
6
314 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 110

Expert Comment

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

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 35

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
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 
LVL 110

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 35

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
The viewer will learn how to dynamically set the form action using jQuery.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

724 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