views:

27

answers:

1

Hi Guys, I have written a pretty basic upload script that takes the file and uploads it using the standard move_uploaded_file method as you see below:

//UPLOAD IMAGE
        $path = "../../clients/$realClient/" . $_FILES["image"]["name"][$x];
        move_uploaded_file($_FILES["image"]["tmp_name"][$x], $path);
        $displayPath = "private/clients/$realClient/" . $_FILES["image"]["name"][$x];       
        mysql_query("INSERT INTO images VALUES(NULL, '$displayPath', '$client', '$project', '$newWeight')") or die("Unable to Insert Image - Please Try Again");
        echo "File Successfully Uploaded<br />";

This upload script works perfectly for most purposes. Here's my issue:

I have a standard shared hosting package so sometimes when the user tries to upload a file that takes over five minutes to upload (say a video or some other high res media), the server times out. The hosting company has said that as it's a shared hosting server they are unwilling to increase the timeout limit for me.

Is there a more efficient upload script that will allow files to go up in under five minutes or is there perhaps an alternative you could suggest?

Cheers, Dan

+2  A: 

The PHP script is run after the upload completes; so if there's a timeout during the upload, there's nothing you can do in the script (as it won't be run at all).

If the timeout occurs during the script, you could split it into multiple parts - have the first script just store the uploaded file and HTTP redirect to another, which will do the processing and database work. In the script you're showing, the processing seems simple enough, not sure if splitting that would help.

Assuming you're showing just a simplified version:

script1.php

    // upload is complete
    $fname= basename($_FILES["image"]["name"][$x]); //prevent directory traversal
    $uniqid = uniqid("",true);
    $path = "../../clients/$realClient/temporary/" . $uniqid . '/' . $fname;
    // move to temporary location and redirect
    move_uploaded_file($_FILES["image"]["tmp_name"][$x], $path);
    header('Location: /script2.php?file=' . $uniqid . '/' . $fname);

script2.php

    $path = $_GET['file'];
    $uniqid = basename(dirname($path));
    $fname = basename($path);
    $temp_path = "../../clients/$realClient/temporary/" . $uniqid . '/' . $fname;
    $final_path = "../../clients/$realClient/" . $fname;
    move($temp_path,$final_path);

    // do whatever processing is necessary
    mysql_query("INSERT INTO images VALUES(NULL, '$displayPath', '$client', '$project', '$newWeight')") or die("Unable to Insert Image - Please Try Again");
    echo "File Successfully Uploaded<br />";
Piskvor
How would I go about doing that?
Daniel Hanly
@Daniel Hanly: added that into the answer.
Piskvor
Thank you for your solution. I'll accept your answer but I also understand that this may not shave that much off the time because of: like you said the PHP is run after the file upload.
Daniel Hanly