获取文件名+;文件夹中图像的路径-PHP
我的脚本指向存储图像的文件夹 我希望检索图像的文件名和路径名,以便在调用时加载图像(请参阅下面的html/php代码) 我尝试了以下操作,但出现错误:获取文件名+;文件夹中图像的路径-PHP,php,image,path,filenames,Php,Image,Path,Filenames,我的脚本指向存储图像的文件夹 我希望检索图像的文件名和路径名,以便在调用时加载图像(请参阅下面的html/php代码) 我尝试了以下操作,但出现错误: Failed to open stream: Permission denied 在这行代码中,$page=file\u获取内容($fileinfo->getPathname()) PHP public function action_mybook($page = '') { FB::log($this->request->
Failed to open stream: Permission denied
在这行代码中,$page=file\u获取内容($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());
}
}
}
<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>
HTML/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());
}
}
}
<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\u get\u contents($fileinfo->getPathname())
和get no errors,并且创建了图像的div,但它表示“加载给定url失败”
使用echo''手动加载图像如果显示图像
尝试为图像授予权限,您可以使用以下方式授予权限:
注意:$fileinfo->getPathname()应返回映像路径。更改777上目录中文件的权限,然后重试尝试注释此行:
$page = file_get_contents($fileinfo->getPathname());
我不知道,您需要将图像文件读取到变量
请检查完整图像路径,尝试以下操作:
$pages[] = 'images/mybook/'.$fileinfo->getFilename();
或
与项目路径相关。可能存在的问题
您的目录分隔符
我尝试执行您的代码并得到相同的代码。为什么?因为/
。在windows中是\
。返回URL无效:
images/mybook\arrows.png
正确答案是:
images\mybook\arrows.png
or images/mybook/arrows.png (linux... in windows works too)
因此,您需要使用PHP的目录分隔符常量,这就解决了您的问题。见下文:
更新
我只是将$page
添加到directoryinterator
中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();
}
}
}
我希望这有帮助
对不起,我的英语不好。您的图像似乎有权限问题。是否确实要将图像代码读取到此变量<代码>$page=file\u获取内容($fileinfo->getPathname())代码>您只需要文件的路径,只要$fileinfo->getPathname(),如果php无法读取文件,它不太可能更改权限…首先:如果他没有读取此文件的权限,他也没有设置chmod的权限。第二,如果您查看作者的代码-他不需要调用文件内容。@BaBL86我认为如果您没有读取文件的权限,您就不能授予该文件的权限。可以使用chmod更改权限。@jeroen我认为这是基于她发生的错误。您应该检查手册:模式只能由在大多数系统上拥有该文件的用户更改代码>。如果php用户拥有文件系统(或者仅仅是文件…),它只会毫无问题地打开文件。这不是个好主意,尤其是对于上传目录。这只是为了调试。如果脚本可以正常工作,那么权限就会出现问题。当我注释这一行时,我没有收到任何错误,它会为图像创建div,但会显示“加载给定url失败”。请尝试以下操作:$pages[]='images/mybook/'.$fileinfo->getFilename()
或$pages[]='/images/mybook/'.$fileinfo->getFilename()
与您的项目路径相关。没有乐趣!它仍然只是在悬停状态下显示它只是说“加载给定url失败”我已经尝试使用echo手动加载图像,效果很好?@Karinamcourty您尝试使用完整路径/url吗?像这样:
。我的意思是,如果退出的图像是image/mybook
,请再试一次。@karinamgourty无论如何,请设置此文件夹的权限:755
,然后重试(手动和自动)。@karinamgourty What-return ofecho$fileinfo->getPathname()代码>。当我添加到主映像文件夹中的文件/文件夹的长列表时,尝试输入此代码并查看结果(如果路径正确或无),例如:karina/images/mybook\arrows.pngkarina/images/mybook\1karina/images/mybook\2karina/images/mybook\3。。。。。。。。