繁体   English   中英

PHP / XAMPP使用DirectoryIterator类不显示图像

[英]PHP/XAMPP Using DirectoryIterator class does not display the images

嗨,我正在使用Wordpress自定义主题,在页脚中,我想显示一系列从文件夹中选择的随机图像。 现在,我已经正确设置了路径,并且路径的输出是正确的。 然后,将路径插入<img>标记中,它仅显示损坏的图像图标。

当我检查时,图像路径正确并且图像名称在那里。 但是,当我单击检查元素框内的图像链接进行查看时,我被拒绝访问! 我尝试在其中显示图像的选项卡上的错误403。 我现在对此进行了广泛的研究,并且尝试了许多不同的方法。

有一次我尝试更改Xampp设置以allow all httpd-vhosts.confhttpd-xampp.conf allow all设置。 这些都不起作用。 然后我开始怀疑这是否可能是WordPress问题,但是尽管我进行了所有研究,但是我不确定如何弄清楚这一点。

这是我的代码片段

$path = dirname(__DIR__, 2) . '/uploads/footer-img/';

//this outputs pathname of C:\xampp7\htdocs\project\wp-content/uploads/footer-img/

foreach ($dir as $fileinfo) {
    if ($fileinfo->isFile()) {

        echo $fileinfo->getBasename() . "\n";
        echo $fileinfo->getBasename('.jpg') . "\n";

        $r = $path . $fileinfo->getBasename();

        //this below outputs my directory with the image.jpg name

        echo $r;

        //this outputs broken image icons

        echo '<img src="' . $r . '"/>'; 

    }
}

您正在混合本地路径和Web路径。

C:\\xamp...是本地路径。 任何与您的文件系统兼容的文件都可以访问它。 您的浏览器无法

http://localhost...是一个Web路径,由您的Web服务器处理。 这是Web服务器的地步 浏览器可以访问它,而与文件系统兼容的东西则不能。

要在页面中显示图像,您需要根据Web服务器配置为其生成Web路径。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM