PHP 读取文件夹只适用于绝对路径
PHP read folder only works with absolute path
我有一个函数可以读取包含图像的文件夹。
问题是它只有在文件夹路径是绝对路径时才有效。
如果我将其更改为动态路径,则会引发错误。
函数如下:
<?php
function getPathsByKind($path,$ext,$err_type = false)
{
# Assign the error type, default is fatal error
if($err_type === false)
$err_type = E_USER_ERROR;
# Check if the path is valid
if(!is_dir($path)) {
# Throw fatal error if folder doesn't exist
trigger_error('Folder does not exist. No file paths can be returned.',$err_type);
# Return false incase user error is just notice...
return false;
}
# Set a storage array
$file = array();
# Get path list of files
$it = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($path,RecursiveDirectoryIterator::SKIP_DOTS)
);
# Loop and assign paths
foreach($it as $filename => $val) {
if(strtolower(pathinfo($filename,PATHINFO_EXTENSION)) == strtolower($ext)) {
$file[] = $filename;
}
}
# Return the path list
return $file;
}
?>
这是我获取它的方式:
<?php
# Assign directory path
//$directory = '/Applications/MAMP/htdocs/domainname/wp-content/themes/themename/images/logos/';
// THIS PART ABOVE IS THE ABSOLUTE PATH AND IS WORKING.
$directory = get_bloginfo('template_directory').'/images/logos/';
$files = getPathsByKind($directory,'svg');
if(!empty($files)) {
for($i=0; $i < 32; $i++){
echo '<img src="'.$files[$i].'">';
}
}
?>
如何让它与相对路径一起工作?
我要用一个问题来回答你的问题:为什么要使用相对路径?
它不能使用相对路径的最可能原因是,当前工作目录不是您认为的那样。您可以使用 getcwd()
函数检查它。
这也是相对路径的最大问题:您 永远 不能依赖它们。可以出于任何原因随时使用 chdir()
从脚本外部设置当前工作目录。
只要您在服务器上处理文件,总是使用绝对路径。如果要解析相对于脚本文件的路径,总是 使用__DIR__
或dirname()
.
在你的例子中,你的代码的问题是你的 getPathsByKind
函数 return 图像的绝对路径,除了 localhost 之外,这对任何人都是无用的。您可以做的是使 getPathsByKind
return 只是文件名而不是完整路径。替换行
$file[] = $filename;
和
$file[] = pathinfo($filename, PATHINFO_BASENAME);
然后,在 img
标签中添加路径:
for($i=0; $i < 32; $i++){
echo '<img src="/images/logos/or/whatever/'.$files[$i].'">';
}
我有一个函数可以读取包含图像的文件夹。
问题是它只有在文件夹路径是绝对路径时才有效。
如果我将其更改为动态路径,则会引发错误。
函数如下:
<?php
function getPathsByKind($path,$ext,$err_type = false)
{
# Assign the error type, default is fatal error
if($err_type === false)
$err_type = E_USER_ERROR;
# Check if the path is valid
if(!is_dir($path)) {
# Throw fatal error if folder doesn't exist
trigger_error('Folder does not exist. No file paths can be returned.',$err_type);
# Return false incase user error is just notice...
return false;
}
# Set a storage array
$file = array();
# Get path list of files
$it = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($path,RecursiveDirectoryIterator::SKIP_DOTS)
);
# Loop and assign paths
foreach($it as $filename => $val) {
if(strtolower(pathinfo($filename,PATHINFO_EXTENSION)) == strtolower($ext)) {
$file[] = $filename;
}
}
# Return the path list
return $file;
}
?>
这是我获取它的方式:
<?php
# Assign directory path
//$directory = '/Applications/MAMP/htdocs/domainname/wp-content/themes/themename/images/logos/';
// THIS PART ABOVE IS THE ABSOLUTE PATH AND IS WORKING.
$directory = get_bloginfo('template_directory').'/images/logos/';
$files = getPathsByKind($directory,'svg');
if(!empty($files)) {
for($i=0; $i < 32; $i++){
echo '<img src="'.$files[$i].'">';
}
}
?>
如何让它与相对路径一起工作?
我要用一个问题来回答你的问题:为什么要使用相对路径?
它不能使用相对路径的最可能原因是,当前工作目录不是您认为的那样。您可以使用 getcwd()
函数检查它。
这也是相对路径的最大问题:您 永远 不能依赖它们。可以出于任何原因随时使用 chdir()
从脚本外部设置当前工作目录。
只要您在服务器上处理文件,总是使用绝对路径。如果要解析相对于脚本文件的路径,总是 使用__DIR__
或dirname()
.
在你的例子中,你的代码的问题是你的 getPathsByKind
函数 return 图像的绝对路径,除了 localhost 之外,这对任何人都是无用的。您可以做的是使 getPathsByKind
return 只是文件名而不是完整路径。替换行
$file[] = $filename;
和
$file[] = pathinfo($filename, PATHINFO_BASENAME);
然后,在 img
标签中添加路径:
for($i=0; $i < 32; $i++){
echo '<img src="/images/logos/or/whatever/'.$files[$i].'">';
}