doupingtang9627 2019-02-21 12:54
浏览 31

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

Hi I am using a Wordpress custom theme, and in my footer I would like to display a series of random images chosen out of a folder. For now I have my path set correctly and my output of the path is correct. Then I insert the path in an <img> tag and it only displays broken image icons.

When I inspect, the image path is correct and the image names are there. However, when I click on the image link inside the inspect element box to view it I get an Access Denied! Error 403 on my tab where I am trying to display the image. I have done extensive research on this now and I have tried so many different approaches.

At one point I tried changing Xampp settings to allow all in httpd-vhosts.conf and httpd-xampp.conf. None of those worked. Then I started wondering if it could be a WordPress issue, but I am not sure how to figure that one out, despite all my research.

Here is a snippet of my code

$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() . "
";
        echo $fileinfo->getBasename('.jpg') . "
";

        $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 . '"/>'; 

    }
}
  • 写回答

1条回答 默认 最新

  • dongyu9263 2019-02-22 06:17
    关注

    You are mixing up local paths and web paths.

    C:\xamp... is a local path. Anything that works with your filesystem can access it. Your browser can't.

    http://localhost... is a web path, handled by your web server. This is the point of webserver. Browser can access it, things that works with filesystem can't.

    To show images in your page you need to be producing web paths for them, according to your web server configuration.

    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值