我正在尝试在浏览器中显示我在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>";
图像成功显示。
我已经验证了vars并且他们有正确的内容。