PHP 备份脚本超时

发布于 2024-11-06 07:05:34 字数 113 浏览 1 评论 0原文

我有一个备份脚本,它将网站的所有文件备份到 zip 文件(使用类似于

有什么方法可以延长脚本运行的时间长度吗?这些网站在共享 Windows 服务器上运行,因此我无权访问 php.ini 文件。

I have a backup script which backups up all files for a website to a zip file (using a script similar to the answer to this question). However, for large sites the script times out before it can complete.

Is there any way I can extend the length of time available for the script to run? The websites run on shared Windows servers, so I don't have access to the php.ini file.

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

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

发布评论

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

评论(7

我的黑色迷你裙 2024-11-13 07:05:34

如果您处于共享服务器环境中,并且无权访问 php.ini 文件,或者想要针对每个站点设置 php 参数,则可以使用 .htaccess文件(在 Apache Web 服务器上运行时)。

例如,要更改 max_execution_time 值,您所需要做的就是编辑 .htaccess(位于网站的根目录中,通常可通过 FTP 访问),然后添加此行:

php_value max_execution_time 300

其中 300 是您希望设置 php 脚本的最大执行时间的秒数。

还有另一种方法是在 php 文件中使用 ini_set 函数,

例如:要将执行时间设置为 5 秒,您可以使用“

ini_set('max_execution_time', 300); //300 seconds = 5 minutes

如果您需要更多说明,请告诉我”。

If you are in a shared server environment, and you don’t have access to the php.ini file, or you want to set php parameters on a per-site basis, you can use the .htaccess file (when running on an Apache webserver).

For instance, in order to change the max_execution_time value, all you need to do is edit .htaccess (located in the root of your website, usually accesible by FTP), and add this line:

php_value max_execution_time 300

where 300 is the number of seconds you wish to set the maximum execution time for a php script.

There is also another way by using ini_set function in the php file

eg. TO set execution time as 5 second, you can use

ini_set('max_execution_time', 300); //300 seconds = 5 minutes

Please let me know if you need any more clarification.

冧九 2024-11-13 07:05:34

我想到了设置时间限制,但可能仍然受到 php.ini 设置

set_time_limit(0) 的限制;

http://php.net/manual/en/function.set-time -limit.php

set time limit comes to mind, but may still be limited by php.ini settings

set_time_limit(0);

http://php.net/manual/en/function.set-time-limit.php

阳光的暖冬 2024-11-13 07:05:34

简单地说;不要发出 HTTP 请求来启动 PHP 脚本。您遇到的边界是因为您使用的是 HTTP 请求而设置的,这意味着您可能会超时。更好的解决方案是使用“cronjob”或微软所说的“计划任务”来实现。大多数托管提供商将允许您在设定的时间运行此类任务。通过从命令行调用脚本,您不必再担心超时,但您仍然面临遇到内存问题的风险。

如果您有一个不错的托管提供商,为什么它不提供每日备份呢? :)

Simply put; don't make a HTTP request to start the PHP script. The boundaries you're experiencing are set because you're using a HTTP request, which means you can have a time-out. A better solution would be to implement this using a "cronjob", or what Microsoft calls "Scheduled tasks". Most hosting providers will allow you to run such a task at set times. By calling the script from command line, you don't have to worry about the time-outs any more, but you're still at risk of running into memory issues.

If you have a decent hosting provider though, why doesn't it provide daily backups to start with? :)

彼岸花似海 2024-11-13 07:05:34

您可以在脚本的开头使用以下内容:

<?php
if(!ini_get('safe_mode')){ 
        set_time_limit(0); //0 in seconds. So you set unlimited time
}
?>

并在脚本的末尾使用 flush() 函数告诉 PHP 发送它生成的内容。

希望这能解决您的问题。

You can use the following in the start of your script:

<?php
if(!ini_get('safe_mode')){ 
        set_time_limit(0); //0 in seconds. So you set unlimited time
}
?>

And at the end of the script use flush() function to tell PHP to send out what it has generated.

Hope this solves your problem.

煮酒 2024-11-13 07:05:34

脚本是否给出“超过 xx 秒的最大执行时间”错误消息,或者是否显示空白页?如果是这样,ignore_user_abort可能就是您正在寻找的。它告诉 php 如果与浏览器的通信丢失,不要停止脚本执行,这可以保护您免受通信中涉及的其他超时机制的影响。

基本上,我会在脚本的开头执行此操作:

set_time_limit(0);
ignore_user_abort(true);

这就是说,正如 Berry Langerak 所建议的,您不应该使用 HTTP 调用来运行备份。您应该使用 cronjob。与 set_time_limit(0) 一起使用,它可以永远运行。

在共享托管环境中,可能不允许更改 max_execution_time 指令,并且您可能无法访问任何类型的命令行,恐怕没有简单(且干净)的方法解决您的问题,最简单的解决方案通常是使用托管服务商提供的备份解决方案(如果有)。

Is the script giving the "Maximum execution time of xx seconds exceeded" error message, or is it displaying a blank page? If so, ignore_user_abort might be what you're looking for. It tells php not to stop the script execution if the communication with the browser is lost, which may protect you from other timeout mechanisms involved in the communication.

Basically, I would do this at the beginning of your script:

set_time_limit(0);
ignore_user_abort(true);

This said, as advised by Berry Langerak, you shouldn't be using an HTTP call to run your backup. A cronjob is what you should be using. Along with a set_time_limit(0), it can run forever.

In shared hosting environments where a change to the max_execution_time directive might be disallowed, and where you probably don't have access to any kind of command line, I'm afraid there is no simple (and clean) solution to your problem, and the simplest solution is very often to use the backup solution provided by the hoster, if any.

廻憶裏菂餘溫 2024-11-13 07:05:34

尝试该功能:

set_time_limit(300);

在 Windows 上,您的网络主机有可能允许您通过上传网络服务器根目录中的 php.ini 文件来覆盖设置。如果是这样,请上传包含以下内容的 php.ini 文件:

max_execution_time = 300

要检查设置是否有效,请执行 phpinfo() 并检查 max_execution_time 的本地值

Try the function:

set_time_limit(300);

On windows, there is a slight possibility that your webhost allows you to over ride settings by uploading a php.ini file in the root directory of your webserver. If so, upload a php.ini file containing:

max_execution_time = 300

To check if the settings work, do a phpinfo() and check the Local Value for max_execution_time.

在你怀里撒娇 2024-11-13 07:05:34

选项 1:要求托管公司将备份放置在 php 可以访问的位置,以便 php 文件可以重定向备份。

选项 2:将备份脚本拆分为多个部分,也许使用一些 ajax 连续调用脚本几次,为用户提供一个漂亮的进度条,并将 zip 中的脚本调用结果与 php 结合起来,并将其提供为下载。

Option 1: Ask the hosting company to place the backups somewhere accesible by php, so the php file can redirect the backup.

Option 2: Split the backup script in multiple parts, perhaps use some ajax to call the script a few times in a row, give the user a nice progress bar and combine the result of the script calls in a zip with php and offer that as a download.

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