views:

48

answers:

2

Hello all, I struggled half a day and came to conclusion that it can't be done. I threw away my php scripts and rewrote it in perl and it worked right from the start the way I wanted it to work. Still, I want to find out if such trivial task can be done correctly in PHP. The question: I have arbitrarily long (in size and time) file upload (via raw data POST) and I simply need to save it to a file with PHP. The way it works with PHP is that it fist fully processes the posted data, saves it to a file and then execution of my script begins (my file upload lasts 30 minutes). Even if I tried to fopen("php:/stdin" or php://input) it still worked that retarded way. What I really need is to be able to process incoming posted data in chunks sequentially. I tried: 1) modphp, 2) php-cgi, 3) php-cli run as a cgi executable. Even though php-cgi is meant to be used a cgi, it still preprocesses posted data (so that $_POST becomes available) and doesn't work just the same way as regular momphp. CLI version run as a cgi script doens't work, as it can't read from php://stdin or php://input at all! Whatever I tried, nothing worked for me and I came to conclusion that it can't be done with PHP... or it can?

thanks

A: 

Of course PHP can do it. Uploaded files are stored in a temporary directory until the PHP script can interact with them. The information necessary to do so is in the $_FILES array.

$field_name = 'file'; # from the HTML form control

$move_result = move_uploaded_file(
    $_FILES[$field_name]['tmp_name'],
    $real_destfile
);

if ($move_result) {
    print "successfully uploaded file (originally called '$_FILES[$field_name]['name']' to $real_destfile";
} else {
    print "failed to receive uploaded file.";
}
amphetamachine
That is not what the OP is asking. He wants to access the file/run his script BEFORE his long upload process finishes.
nico
A: 

it sounds like you want to stream data. to do that you would have to set up a socket that is ready to recive data as a stream.. ie replace the apache web server.

Your problem is not the language, it is the web server. the web server listens to port 80, and then does what it does. Eg write the post data to file then start the php script.

you can create a custom port program like you did with Pearl. you could also do the same thing with PHP, and listen to port 80, using a command line driven php script, rather than one that is nestled inside apache. But PHP is not the best language to do that with.. I would suggest Python.

There is an overhead attached to HTTP communications, prehaps a leaner protocol would be of use to you, it may reduce your upload time.

if your upload is taking so long, you could do it in an AJAX method, and let it continue whilst your client application is still active. the client AJAX application could also do the work of dividing it into chunks.

Bingy
Yes, I'm streaming data. The reason I don't want to use $_FILES is that it doesn't work well with BIG and LONG uploads (which streaming to the server looks like to PHP). Command line PHP is the worst idea ever, seriously I can't believe how bad it was! I really like php, but when I tried to use it in CLI mode I realized that it's a pile of crap: it throw too many errors and I couldn't see any logs. I ended up writing a perl cgi script. It's better than opening a socket: apache calls cgi script and hands over all socket handling to the cgi program.
Pavel
I don't know Python at all, so it's no my solution. I found how to do it (via cgi-perl) and I know about all that tcp-listeners ways to do it, but I really want to know if that can be done with php by handling raw input/post data. It's kind of very useless to have php://input which may make some believe that it allows direct access to input stream.
Pavel