PHP 备份脚本超时
我有一个备份脚本,它将网站的所有文件备份到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果您处于共享服务器环境中,并且无权访问 php.ini 文件,或者想要针对每个站点设置 php 参数,则可以使用
.htaccess
文件(在 Apache Web 服务器上运行时)。例如,要更改
max_execution_time
值,您所需要做的就是编辑.htaccess
(位于网站的根目录中,通常可通过 FTP 访问),然后添加此行:其中 300 是您希望设置 php 脚本的最大执行时间的秒数。
还有另一种方法是在 php 文件中使用 ini_set 函数,
例如:要将执行时间设置为 5 秒,您可以使用“
如果您需要更多说明,请告诉我”。
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: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
Please let me know if you need any more clarification.
我想到了设置时间限制,但可能仍然受到 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
简单地说;不要发出 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? :)
您可以在脚本的开头使用以下内容:
并在脚本的末尾使用 flush() 函数告诉 PHP 发送它生成的内容。
希望这能解决您的问题。
You can use the following in the start of your script:
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.
脚本是否给出“超过 xx 秒的最大执行时间”错误消息,或者是否显示空白页?如果是这样,ignore_user_abort可能就是您正在寻找的。它告诉 php 如果与浏览器的通信丢失,不要停止脚本执行,这可以保护您免受通信中涉及的其他超时机制的影响。
基本上,我会在脚本的开头执行此操作:
这就是说,正如 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:
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.尝试该功能:
在 Windows 上,您的网络主机有可能允许您通过上传网络服务器根目录中的 php.ini 文件来覆盖设置。如果是这样,请上传包含以下内容的 php.ini 文件:
要检查设置是否有效,请执行
phpinfo()
并检查 max_execution_time 的本地值。Try the function:
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:
To check if the settings work, do a
phpinfo()
and check the Local Value for max_execution_time.选项 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.