3

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
  • Ha, cheers, but unfortunately it's my work's responsibility not my own and they wont switch Commented Sep 15, 2010 at 9:56
  • That may put you between a rock and a hard place, as you may not have a way to fix this. Commented Sep 15, 2010 at 10:15

1 Answer 1

2

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 />";
Sign up to request clarification or add additional context in comments.

3 Comments

How would I go about doing that?
@Daniel Hanly: added that into the answer.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.