views:

48

answers:

3

Hi there.

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'){

A: 

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.

Yasen Zhelev
A: 

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
        )

)
maggie
Hmm this didn't work from the page I was working on, so I created a dummy page with just a single upload form, and it works fine. Something on this page is crapping over my upload. *shrugs*. I'll let y'all know when/if I figure it out. Thanks.
tomh
OK, it now seems to be going through. Adding `ignore="yes"` on the end of my submit button did it.
tomh
:/ hym... validate your markup, or link the complete upload-page markup so we can test it.. perhaps we'll run into same problem (did you test it from different browsers?)
maggie
To be honest, I am not surprised. I was trying to integrate this page into our admin interface thing here at work, which is an utter mess of unnecessary javascript and stuff which overwrites markup and omits certain things as it sees fit. It wouldn't be worth anyone's time. Thanks though.
tomh
A: 

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.

Marc B