views:

34

answers:

0

Hello,

I have been working on adding functionality to a site originally written in PHP 4.4.9. It's not in their budget to port the site to PHP5, so don't even suggest it. (Although it needs it badly). The problem I am facing is how to copy binary data from a GET request to a file location on the server. The code that is currently written to support this method is as follows:

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);
}

The problem that I am having with this is the funciton stream_copy_to_stream is only supported in PHP 5. Here is what I have so far, but it seems to only create files that are 8K in size and I'm not sure why. It should, in theory, allow for up to 20M.

function save($path) {    
    $input = fopen("php://input", "rb");
    $temp = tmpfile();
    fwrite($temp, fread($input, 20971520));
    fclose($input);

    $target = fopen($path, "w");        
    fseek($temp, 0, SEEK_SET);
    #stream_copy_to_stream($temp, $target);
    fwrite($target, fread($temp, 20971520));
    fclose($target);
    echo $path;

    return true;
}

I removed the size check because I couldn't figure a way to get the filesize when reading. Any help is greatly appreciated on this. I have been racking my brain for literally hours and I know there is someone out there, most likely on stackoverflow, that can answer my question probably very easily.

Thanks for all the help in advance!

Also, I am submitting data via GET to to allow multiple file uploads with progress bars, etc.