duanliang2017 2017-10-24 09:30
浏览 39
已采纳

PHP读取文件夹仅适用于绝对路径

I have a function that reads out a folder containing images.
The problem is that it only works if the path to the folder is absolute.

If I change it to a dynamic path it throws me an error.

Here is the function:

<?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;
}
?>

and here is how I fetch it:

<?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].'">';
    }
}
?>

How can I make it work with a relative path?

  • 写回答

1条回答 默认 最新

  • doulu2591 2017-10-24 09:38
    关注

    I'm gonna answer your question with a question: why should it work with a relative path?

    The most likely reason why it doesn't work with a relative path is, the current working directory is not what you think it is. You can check it with getcwd() function.

    This is also the biggest problem with relative paths: you can NEVER rely on them. Current working directory can be set from outside the script with chdir() any time for any reason.

    Whenever you work with files on your server, ALWAYS use absolute paths. If you want to resolve a path relative to the script file, ALWAYS use __DIR__ or dirname().

    In your case, the problem with your code is that your getPathsByKind function returns absolute paths to the images, which are useless for anyone except localhost. What you can do is make getPathsByKind return just file names instead of full paths. Replace line

    $file[] =   $filename;
    

    with

    $file[] = pathinfo($filename, PATHINFO_BASENAME);
    

    then, prepend the path in img tag:

    for($i=0; $i < 32; $i++){
        echo '<img src="/images/logos/or/whatever/'.$files[$i].'">';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里