PHP file uploading headache – Education Career Blog

I am having a tough time trying to upload files through PHP.

My form:

<form action="blah.php" enctype="multipart/form-data" method="post">
<p> Upload file: <input type="file" name="xmlfile"/>
<input type="submit" name="upload_submit" value="Upload" /> </p>
</form>

Checklist:

  • No ‘smart’ quotes in sight. Fine.

  • Proper enctype. Fine.

  • name attrib in input tag. Fine.

  • My /tmp directory has the following permissions: drwxrwxrwt. Fine.

  • post_max_size = 50M, upload_max_filesize = 50M, file_uploads = On. Fine.

print_r($_FILES) gives Array(). Useless. Tried on images, xml files, etc. Nothing works.

What I don’t understand even further is that there are pages on which file uploading works on the same server. The only thing different from what I can gather is that the page I am working on has a few other forms which aren’t of enctype="multipart/form-data". Should this matter?

PHP code as requested:

if($_SERVER'REQUEST_METHOD' == 'POST'){
    if(isset($_POST'upload_submit')){
        print_r($_FILES);
        exit();

        ...
    }
}

Gives an empty array regardless of print_r‘s position; I also tried it right after the if($_SERVER'REQUEST_METHOD' == 'POST'){

,

Are you sure that you are submitting the right form or you are dealing with the submitted data in the right place/script? Provide some of the PHP code please.

,

your from, this php script (as blah.php)

edit for debugging, dump $_POST if is false

error_reporting(E_STRICT);
ini_set('display_errors', 'on');

if(array_key_exists('xmlfile', $_FILES)) {
   echo 'file ' , $_FILES'xmlfile''name' , ' recieved';
   echo '<pre>'
      , print_r($_POST, true)
      , print_r($_FILES, true)
      , '</pre>';
} else {
   echo '<pre>'
      , print_r($_POST, true)
      , '</pre>';
} 

(possible) output:

file rapid-gui-development-with-qtruby_p1_4.mobi recieved
Array
(
    upload_submit => Upload
)
Array
(
    xmlfile => Array
        (
            name => rapid-gui-development-with-qtruby_p1_4.mobi
            type => application/octet-stream
            tmp_name => /private/tmp/phpEyV3vy
            error => 0
            size => 556846
        )

)

,

You mention having other forms on the page… are those properly closed? For instance, if you have:

<form method="post">
    blah blah blah
    <input type="submit" />
<!-- oops forgot to </form> here -->
<form method="post" enctype="multipart/form-data">
    ...
    <input type="submit" />
</form>

Then the FIRST <form> tag could be taking precendence and submitting without the enctype set.

If you’re on Firefox, I’d suggest using Firebug/HTTPFox/LiveHTTPHeaders (all available in FF’s add-ons library) to see what’s being sent over the wire, and running your page through a validator to make sure there’s no goofy HTML bugs causing this.

Leave a Comment