1

我正在尝试通过浏览器中的函数显示我的codeigniter代码目录中的所有图像,因此我使用以下代码:

$imgdir = 'upload/'; //Pick your folder
$allowed_types = array('png','jpg','jpeg','gif'); //Allowed types of files
$dimg = opendir($imgdir);//Open directory
while($imgfile = readdir($dimg))
{

if( in_array(strtolower(substr($imgfile,-3)),$allowed_types) OR
in_array(strtolower(substr($imgfile,-4)),$allowed_types) )
/*If the file is an image add it to the array*/
{$a_img[] = $imgfile;}
}
echo "<ul>";

$totimg = count($a_img);  //The total count of all the images
//Echo out the images and their paths incased in an li.
for($x=0; $x < $totimg; $x++){echo "<li><img src='http://localhost/code/'" . $imgdir . $a_img[$x] . "' /></li>"; echo $imgdir . $a_img[$x];} 
echo "<li><img src='http://localhost/code/upload/corrosivo.jpg' /></li>";
echo "</ul>";

我不明白的是为什么我在以下浏览器中得到损坏的图像框:

echo "<li><img src='http://localhost/code/'" . $imgdir . $a_img[$x] . "' /></li>"; echo $imgdir . $a_img[$x];}

与:

echo "<li><img src='http://localhost/code/upload/corrosivo.jpg' /></li>";

图像显示成功。

我已经验证了变量并且它们具有正确的内容。

4

0 回答 0