PHP 上传超时:更高效的上传脚本?

发布于 2024-09-24 00:17:35 字数 760 浏览 2 评论 0原文

我编写了一个非常基本的上传脚本,它获取文件并使用标准 move_uploaded_file 方法上传它,如下所示:

//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 />";

此上传脚本非常适合大多数用途。这是我的问题:

我有一个标准的共享托管包,因此有时当用户尝试上传需要超过五分钟才能上传的文件(例如视频或其他一些高分辨率媒体)时,服务器会超时。托管公司表示,由于它是共享托管服务器,他们不愿意为我增加超时限制。

是否有更有效的上传脚本可以让文件在五分钟内上传,或者您是否可以建议其他替代方案?

干杯, 担

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梅窗月明清似水 2024-10-01 00:17:35

PHP 脚本在上传完成后运行;因此,如果上传过程中超时,则脚本中您无能为力(因为它根本不会运行)。

如果脚本期间发生超时,您可以将其分成多个部分 - 让第一个脚本仅存储上传的文件并将 HTTP 重定向到另一个脚本,该脚本将执行处理和数据库工作。在您显示的脚本中,处理似乎足够简单,不确定拆分是否有帮助。

假设您仅显示简化版本:

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 />";

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 />";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文