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 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line