2013-03-18 60 views
0

我有以下脚本,用来为PDF网站上的某些页面:拆分PHP脚本来避免超时

<? 
    include_once('phpToPDF.php') ; 
    phptopdf_url('http://www.example.com/csb04-termination-box/?style=alt' ,'pdf/', 'csb04-termination-box.pdf'); 
    phptopdf_url('http://www.example.com/csb05-termination-box/?style=alt' ,'pdf/', 'csb05-termination-box.pdf'); 
    phptopdf_url('http://www.example.com/csb06-compact-termination-box/?style=alt' ,'pdf/', 'csb06-compact-termination-box.pdf'); 
    echo 'Done'; 
?> 

我的问题是,如果我添加更多的网页服务器超时脚本之前已经完成运行。我试过更改set_time_limit(0);,但这并没有帮助(在共享主机上)。

修改脚本的最佳方法是什么?我考虑尝试将其分割成多个脚本并通过ajax运行它,但不知道从哪里开始,因为之前没有经验。

回答

3

最简单的方式做这将是这样的:

include_once('phpToPDF.php') ; 
$i = isset($_GET['i']) ? intval($_GET['i']) : 0; 

if ($i == 0) phptopdf_url('http://www.example.com/csb04-termination-box/?style=alt' ,'pdf/', 'csb04-termination-box.pdf'); 
if ($i == 1) phptopdf_url('http://www.example.com/csb05-termination-box/?style=alt' ,'pdf/', 'csb05-termination-box.pdf'); 
if ($i == 2) phptopdf_url('http://www.example.com/csb06-compact-termination-box/?style=alt' ,'pdf/', 'csb06-compact-termination-box.pdf'); 

if ($i < 3) header('Location: ?i=' . ($i + 1)); 
echo 'Done'; 

基本上,这可以让你的网页浏览器执行计数,不使用Ajax。当一个请求完成后,浏览器自动加载下一页,直到计数器($i)是在3

+0

非常确实很好。 – Bart 2013-03-18 17:31:14

+0

完美的作品,谢谢! – 2013-03-18 17:36:57

+0

有没有一种方法可以轻松地在屏幕上包含进度更新?就像echo'1/3'处理完第一个之后等等... – 2013-03-19 09:18:52

0

使用下面的脚本来处理所有的文件,一次一个:

include_once('phpToPDF.php') ; 

$urls = array(); 
$urls[] = array('http://www.example.com/csb04-termination-box/?style=alt', 'csb04-termination-box.pdf'); 
$urls[] = array('http://www.example.com/csb05-termination-box/?style=alt', 'csb05-termination-box.pdf'); 
$urls[] = array('http://www.example.com/csb06-termination-box/?style=alt', 'csb06-termination-box.pdf'); 
// add here more elements in $urls if needed 

if(!isset($_GET['n'])) 
    $n = 0; 
else 
    $n = intval($_GET['n']); 

if(isset($urls[$n])) 
{ 
    phptopdf_url($urls[$n][0] ,'pdf/', $urls[$n][1]); 
    header('Location: ?n='.($n+1)); // calls the conversion of the next file in the $urls array 
} 
else 
    echo 'Done';