【问题标题】:Get file name + path of images in folder - PHP获取文件夹中图像的文件名+路径 - PHP
【发布时间】:2014-02-12 16:30:46
【问题描述】:

我的脚本指向一个存储图像的文件夹。

我想检索图像的文件名和路径名,以便在调用时加载我的图像(参见下面的 html/php 代码)。

我尝试了以下方法,但出现错误:

Failed to open stream: Permission denied

在这行代码$page = file_get_contents($fileinfo->getPathname());

PHP

public function action_mybook($page = '') {

    FB::log($this->request->param('id1'));
    $this->template->content = View :: factory('mybook/default');
    // give me a list of all files in folder images/mybook_images
    $dir = new DirectoryIterator('images/mybook/');
    $this->template->content->pages = array('.$dir.');
    foreach ($dir as $fileinfo) {
        if (!$fileinfo->isDot()) {
            $pages[] = $fileinfo->getFilename();
            $page = file_get_contents($fileinfo->getPathname());
        }
    }
  }

HTML/PHP

<div id="book">
        <!-- Next button -->
        <div ignore="1" class="next-button"></div>
        <!-- Previous button -->
        <div ignore="1" class="previous-button"></div>
        <?php
        foreach ($pages as $page) {
            echo '<div><img src="'.$page.'" /></div>';
        }
        ?>
    </div>

如果我注释掉 $page = file_get_contents($fileinfo-&gt;getPathname()); 行并且没有出现错误,并且图像的 div 已创建,但它显示“无法加载给定的 url”

使用echo '&lt;img src="myimage.png"&gt;' it displays the image手动加载图片

【问题讨论】:

  • 您的图片似乎有权限问题。
  • 你真的要读取图像代码到这个变量吗? $page = file_get_contents($fileinfo-&gt;getPathname());你只需要文件的路径,$fileinfo->getPathname()

标签: php image path filenames


【解决方案1】:

可能的问题

您的目录分隔符

我尝试执行您的代码并获得相同的代码。为什么?因为/。在 Windows 中是\。返回网址无效:

images/mybook\arrows.png

正确的是:

images\mybook\arrows.png

or images/mybook/arrows.png (linux... in windows works too)

所以,你需要使用 PHP 的DIRECTORY_SEPARATOR 常量,这解决了你的问题。见下文:

更新

我只是将$page 添加到DirectoryIterator 中的URL 末尾。

public function action_mybook($page = '') {

  FB::log($this->request->param('id1'));
  $this->template->content = View :: factory('mybook/default');

  $dir = new DirectoryIterator('images' . DIRECTORY_SEPARATOR . 'mybook' . DIRECTORY_SEPARATOR . $page);
  $this->template->content->pages = array('.$dir.');

  foreach ($dir as $fileinfo) {

    if (!$fileinfo->isDot()) {
      $pages[] = $fileinfo->getPathname();
    }

  }

}

希望对你有所帮助。

对不起我的英语。

【讨论】:

  • 我曾尝试仅使用 echo '' 手动加载图像,它工作正常吗?
  • @KarinaMcGourty 您尝试使用完整路径/url?像这样:&lt;img src="yoursite.com/images/mybook/myimage.png" alt="myimage"&gt;。我的意思是,如果image/mybook 的现有图像,而不是其他图像,请尝试。
  • @KarinaMcGourty 不管怎样,设置这个文件夹的权限:755,然后再试一次(手动和自动)。
  • @KarinaMcGourty echo $fileinfo-&gt;getPathname()); 的回报是什么。尝试输入此代码并查看结果(如果获取正确的路径或没有)
  • 当我添加时,我得到了一长串位于该主图像文件夹中的文件/文件夹,例如:karina/images/mybook\arrows.pngkarina/images/mybook\1karina/images /mybook\2karina/images/mybook\3........
【解决方案2】:

尝试评论这一行:

$page = file_get_contents($fileinfo->getPathname());

我不知道,您需要将图像文件读取到变量。

检查您的完整图像路径,试试这个:

$pages[] = 'images/mybook/'.$fileinfo->getFilename();

$pages[] = '/images/mybook/'.$fileinfo->getFilename();

与您的项目路径有关。

【讨论】:

  • 当我注释掉这一行时,我没有收到任何错误,它会为图像创建 div,但它显示“无法加载给定的 url”
  • 试试这个:$pages[] = 'images/mybook/'.$fileinfo-&gt;getFilename()$pages[] = '/images/mybook/'.$fileinfo-&gt;getFilename() 与您的项目路径相关。
  • 不高兴!它仍然只是显示
    悬停它只是说'无法加载给定的 url'
【解决方案3】:

更改 777 目录中文件的权限,然后重试

【讨论】:

  • 坏主意,尤其是上传目录。
  • 仅用于调试。如果脚本可以正常工作,那么权利问题。
【解决方案4】:

尝试为您的图片授予权限,您可以使用chmod 授予权限:

chmod($fileinfo->getPathname(),0777);//add this line in your code
$page = file_get_contents($fileinfo->getPathname());

注意:$fileinfo->getPathname() 应该返回图片路径。

【讨论】:

  • 如果php无法读取文件,则不太可能更改权限...
  • 起初:如果他没有权限读取这个文件 - 他也没有权限设置 chmod。其次,如果您查看作者的代码 - 他不需要调用 file_get_contents。
  • @BaBL86 我不认为如果您没有读取文件的权限,则不能授予该文件的权限。可以使用 chmod 更改权限。
  • @jeroen 我认为这是基于她发生的错误。
  • 您应该查看手册:The mode can be changed only by user who owns the file on most systems. 。如果 php 用户拥有文件系统(或只是文件......),它只会打开文件而没有任何问题。
猜你喜欢
  • 2012-04-29
  • 2021-02-05
  • 2019-07-13
  • 2015-06-15
  • 1970-01-01
  • 2016-06-03
  • 2011-07-31
  • 2011-01-25
相关资源
最近更新 更多