【发布时间】:2015-08-28 22:11:36
【问题描述】:
我编写了一个简短的 php 代码,它显示一个图像和目录中每个文件的名称。问题是,如果我运行代码,许多文件会得到 a 而不是图像。因此,例如 .zip 文件位于 .txt 文件的前面。现在它将显示 txt 文件而不是 .zip 图像。
代码:
$file_dir = "upload/demofiles";
if ($handle = opendir($file_dir)) {
$i = 1;
while (false !== ($entry = readdir($handle))) {
$format = pathinfo($entry, PATHINFO_EXTENSION);
$file_path = $file_dir.'/'.$entry;
if($format == "txt"){
$myfile = fopen($file_path, "r") or die("Unable to open file!");
$file_element = '<iframe src="'.$file_path.'" width="80px" style="height:80px" scrolling="no"></iframe>';
}
if($format == "png"){
$myfile = fopen($file_path, "r") or die("Unable to open file!");
$file_element = '<img src="'.$file_path.'" />';
}
if($format == "zip"){
$myfile = fopen($file_path, "r") or die("Unable to open file!");
$file_element = '<img width="64px" class="img" src="icons/zip.png" />';
fclose($myfile);
}
if($format == "pdf"){
$file_element = '<embed src="'.$file_path.'" width="80px" scrolling="no" style="height:80px">';
}
if(!isset($file_element)){
$file_element = '<img class="img" width="64px" src="icons/file.png">';
}
if ($entry != "." && $entry != "..") {
echo '<label style="display:inline-block; border:solid 1px;">
<div>'.$file_element.'</div>
<div>'.$entry.'</div>
</label>';
}
$i++;
}
}
为什么会这样?我认为应该在循环运行后清除变量。
谢谢!
【问题讨论】:
-
为什么会这样?
"The problem is that if i run the code many files get the a instead of an image"...得到一个??
标签: php loops iframe while-loop readdir