Php 当前目录中的图像未显示
我正在尝试使用php脚本显示当前目录中的所有图像。 显示图像的php文件也在同一目录中。 这是剧本Php 当前目录中的图像未显示,php,Php,我正在尝试使用php脚本显示当前目录中的所有图像。 显示图像的php文件也在同一目录中。 这是剧本 <?php $dir =basename(__DIR__); if (file_exists($dir) == false) { echo 'Directory \''. $dir. '\' not found!'; } else{ $dir_contents = scandir($dir); foreach ($dir_contents as $file) {
<?php
$dir =basename(__DIR__);
if (file_exists($dir) == false)
{
echo 'Directory \''. $dir. '\' not found!';
}
else{
$dir_contents = scandir($dir);
foreach ($dir_contents as $file)
{
$file_type = strtolower(end(explode('.', $file)));
if ($file !== '.' && $file !== '..' && in_array($file_type, $file_display) == true)
{
echo '<img src="'.$dir. '/'.$file. '" alt="'.$file. '" />'; } } }
?>
输出显示不存在目录。但是我看到了目录的存在。请告诉我我的密码有什么问题
谢谢我已经改写了你的脚本。使用
glob()
获取文件,使用pathinfo()
获取文件扩展名
$files = glob(__DIR__.DIRECTORY_SEPARATOR.'*');
foreach ($files as $file) {
if (!is_file($file)) { // if isn't file, skip it
continue;
}
$info = pathinfo($file);
// check if allowed extensions
if (in_array(strtolower($info['extension']), array('jpg', 'png', 'gif'))) {
echo ...; // your image echo
}
}
如果所有图像都是例如jpg
$files = glob(__DIR__.DIRECTORY_SEPARATOR.'*.jpg');
然后跳过部分,检查扩展名。我的代码有什么问题?老实说,几乎所有的东西都有问题。谢谢@M.Kebza的回复,我使用了上面的代码,但它显示为空白屏幕。这是图片echo中php文件的链接,我做了这个echo“”;是吗?在你提供的链接上我可以看到图像。。。一切都好吗?