Solved

$_FILES not set?

Posted on 2012-03-12
6
298 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
Comment Utility
No, you cannot force your client to upload files.  You cannot prepopulate the $_FILES array.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn how to count occurrences of each item in an array.

771 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

10 Experts available now in Live!

Get 1:1 Help Now