PHP download timeouts: more efficient Script loading?

I wrote a fairly basic download script that takes a file and loads 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 script download works great for most purposes. Here is my problem:

I have a standard package of shared hosting, so sometimes when a user tries to download a file that takes more than five minutes to load (say, video or other high resolution), the server does not work. The hosting company said that since it is a public server, they do not want to increase the timeout limit for me.

Is there a more efficient script loading that will allow files to go up in less than five minutes, or is there an alternative you could offer?

Cheers, dan

+3
source share
1 answer

PHP script is launched after the download is completed; therefore, if there is a timeout at boot time, you cannot do anything in the script (since it will not be run at all).

If the timeout occurs during the script, you can break it into several parts - first the first script will simply save the downloaded file and redirect HTTP to another, which will do the processing and work with the database. In the script you are showing, the processing seems simple enough, not sure if this will help.

Assuming you are showing only 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 />";
+2
source

Source: https://habr.com/ru/post/1764852/


All Articles