views:

125

answers:

2

Hi,

I'm having an issue on my development machine which seems isolated to this machine and can't figure it out. I have a jQuery file uploader which posts the user selected file to a php script for handling using XmlHttpRequest. The script works fine on my MacBook Pro running OSX10.6.3 with MAMP 1.9, however on my iMac with the exact same operating system and version of MAMP, with an identical server image, it fails.

I have traced the cause of the error down to the property $_SERVER["CONTENT_LENGTH"] returning 0, even though I can get the filename just fine and everything else seems to have suceeded about the request. For some reason it just won't seem to give me the actual content length. Here is the code that is causing the problem - the function in question is getSize().

class qqUploadedFileXhr {
    /**
     * Save the file to the specified path
     * @return boolean TRUE on success
     */
    function save($path) {    
        $input = fopen("php://input", "r");
        $temp = tmpfile();
        $realSize = stream_copy_to_stream($input, $temp);
        fclose($input);

        if ($realSize != $this->getSize()){            
            return false;
        }

        $target = fopen($path, "w");        
        fseek($temp, 0, SEEK_SET);
        stream_copy_to_stream($temp, $target);
        fclose($target);

        return true;
    }
    function getName() {
        return $_GET['qqfile'];
    }
    function getSize() {
        if (isset($_SERVER["CONTENT_LENGTH"])){
            return (int)$_SERVER["CONTENT_LENGTH"]; //*THIS* is returning 0            
        } else {
            throw new Exception('Getting content length is not supported.');
        }      
    }   
}

Any help would be much appreciated.

Sam

A: 

Did you set your encoding type to multipart/form-data ?

<form action="upload.php" method="post" enctype="multipart/form-data">
  ...
  <input type="file" ... />
  ...
</form>
Yanick Rochon
Yes this is handled by the jQuery plugin, and the encoding is correct.
Sam
+1  A: 

Solved it! Seems the jQuery script I am using fails under firefox 3.5.x, I updated to 3.6.9 and it works fine. Now I have to find some way to make it backwards compatible with older versions of firefox.

Sam