Solved

$_FILES not set?

Posted on 2012-03-12
6
306 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 109

Expert Comment

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

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 109

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
php extract($_REQUEST) 5 47
ASP.NET(C#) Eliminating weekends from a date range 2 34
How would I do this...? 2 27
JavaScript let vs var 5 21
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 …
Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

776 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