好吧,我下面的代码一次创建了所有的文件,而不是在上依次经过1、2、3、4
$i = 0;
while($i < $pages)
{
$met = $this->articles();
$myFile = trim($met.".php");
$fh = fopen($myFile, 'w') or die("Can't create files.");
$stringData = "<html><head><meta name='"description'" content='"" . $met. "'" />
<meta name='"keywords'" content='"" . $met . "'" /><h1>" . $met . "</h1>
<base href='http://gumpic.com/'>".$this->show_web("http://gumpic.com")."</html>";
fwrite($fh, $stringData);
fclose($fh);
$i++;
}
echo "We've created your " . $pages . " pages.";
我该如何加载页面,在页面上打印哪些页面已经完成?喜欢
- 完成
- 完成
- 完成
- 挂起
还有,是不是无论如何我都可以加快这个脚本。它非常慢,只在大约3分钟内创建了2k个文件,然后给了我一个内部错误。
此脚本的速度主要取决于硬盘驱动器的速度。还有操作系统处理打开和写入访问的效果。
如果要显示进度,只需在while
循环中包含一个echo("Processing ".$i."'n");
,并用flush()
刷新缓冲区。确保在脚本启动时发送echo str_repeat(" ", 1024), "'n";
,以强制浏览器显示内容,而无需等待大量数据。
使用set_time_limit()
可以给脚本更多的处理时间。
考虑使用一些模板语言,因为每次分配和链接输出肯定会带来糟糕的性能。
一些好的库是:
- Twig[我通常的选择]
- 聪明
- TI[纯PHP,少量用于较小用途]
若你们还并没有写命令行工具的话,可以考虑写一个命令行工具——回显到控制台会比写到屏幕更快。
ob_start();
$i = 0;
while($i < $pages)
{
$met = $this->articles();
$myFile = trim($met.".php");
$fh = fopen($myFile, 'w') or die("Can't create files.");
$stringData = '<html>
<head>
<meta name="description" content="' . $met. '" />
<meta name="keywords" content="' . $met . '" />
</head>
<body>
<h1>"' . $met . '"</h1>
<base href="http://gumpic.com/">'.$this->show_web("http://gumpic.com").'</body>
</html>';
fwrite($fh, $stringData);
fclose($fh);
echo 'Created page ' . ($i+1) . '<br/>';
flush();
ob_flush();
$i++;
}
echo "Done generating " . $i . "pages.";