PHP脚本运行不稳定


PHP script keeps running indefinetly

我添加了以下代码来显示目录中的文件

$exclude = array("index.php","cssheadertop.php","cssheaderbottom.php");
$cssfiles = array_diff(glob("*.php"), $exclude); 
for($i=0;$i<=5;$i++){
    $filename = "http://8mags.com/lessons/css/".$cssfiles[$i];
    outputtags($filename,true,true); 
}

我不知道为什么,但它一直在无限期地运行。函数CCD_ 1没有问题,因为下面的代码工作得很好

$exclude = array("index.php","cssheadertop.php","cssheaderbottom.php");
$cssfiles = array_diff(glob("*.php"), $exclude); 
for($i=0;$i<=5;$i++){
    $cssfile = array_rand($cssfiles);
    $filename = "http://8mags.com/lessons/css/".$cssfiles[$cssfile];
    outputtags($filename,true,true); 
}

工作非常完美。

这是输出的代码

function outputtags($filename,$other,$programming)
{
$html = file_get_contents_curl($filename);
$doc = new DOMDocument();
@$doc->loadHTML($html);
$nodes = $doc->getElementsByTagName('title');
$title = $nodes->item(0)->nodeValue;
$metas = $doc->getElementsByTagName('meta');
for ($i = 0; $i < $metas->length; $i++)
{
$meta = $metas->item($i);
if($meta->getAttribute('property') == 'og:title')
    $ogtitle = $meta->getAttribute('content');
if($meta->getAttribute('property') == 'og:image')
    $ogimage = $meta->getAttribute('content');
if($other)
{if($meta->getAttribute('property') == 'og:description')
    $ogdescription = $meta->getAttribute('content');}
}
echo '<p style="margin:0;"><a href='.$filename.' target=_blank>'.$ogtitle.'</a></p>';
if(!$other)
echo '<a href='.$filename.' target=_blank><img style="margin:0 0 40px 0;" src="'.$ogimage.'" alt=""></a></br>';
if($other)
{
if(!$programming)
echo '<a href='.$filename.' target=_blank><img src="'.$ogimage.'" alt=""></a></br>';
echo '<p style="margin:0 0 40px 0;">'.$ogdescription.'</p>';
}
}

按照评论中的建议,在写完exit;后进行编辑,我得到了

array(8) { [0]=> string(12) "3dbutton.php" [1]=> string(15) "basicbutton.php" [2]=> string(19) "basictextshadow.php" [5]=> string(11) "cssmenu.php" [6]=> string(21) "csstexteffectlogo.php" [7]=> string(22) "glossyroundbuttons.php" [8]=> string(18) "glowtextshadow.php" [10]=> string(26) "transitionhoverbuttons.php" } 

最后在使用foreach脚本后仍然保持运行很长一段时间的任何建议

foreach ($cssfiles as $cssfile) {
$filename = "http://8mags.com/lessons/css/".$cssfiles[$cssfile];
outputtags($filename,true,true); 
}

您的数组键为0,1,2,5,6,7等。。。这就是为什么它在不使用随机时会中断,因为它正在寻找键3和4。您需要对数组重新设置关键帧,或者使用"for each",而不是用数字迭代关键帧。

$exclude = array("index.php","cssheadertop.php","cssheaderbottom.php");
$cssfiles = array_diff(glob("*.php"), $exclude); 
for($i=0;$i<=5;$i++){
    $filename = "http://8mags.com/lessons/css/".$cssfiles[$i];
    outputtags($filename,true,true); 
}

运行脚本时,

$filename = "http://8mags.com/lessons/css/".$cssfiles[3];
$filename = "http://8mags.com/lessons/css/".$cssfiles[4];

不存在,所以它断了。

您正在将http://8mags.com/lessons/css/发送到输出()。