PHP:glob(realpath())读取目录,但路径不正确


PHP: glob( realpath() ) reads directory but paths are incorrect

我不知道我在这里没有得到什么,但我正在尝试用php读取一个目录,并创建一个JS数组,其中包含所有图像路径。

现在,我在本地服务器上有以下结构。

  • 项目
      • index.php
      • imgs
        • image1.png
        • image2.png

所以我现在正在使用我的index.php。我只想读取与`index.php`相同目录中的imgs文件夹

$images = glob( realpath('img')."/*.png" );
print_r($images);
// just for testing purposes / creating a JS array later
foreach( $images as $image ):
    echo "<img src='" . $image . "' />";
endforeach;

print_r函数发出这个…

Array ( 
    [0] => /Users/my/htdocs/test.com/project/one/img/image1.png 
    [1] => /Users/my/htdocs/test.com/project/one/img/image2.png 
) 

对我来说,这似乎还可以,但路径似乎不正确,因为它们并没有真正链接到图像。难道不可能在某种程度上使用相对路径吗?我的意思是,我只需要得到imgs/image1.png,而不是绝对路径。

你知道我在这里做错了什么吗?

去掉realpath('img')如果在glob参数中包含完整路径,则会在结果中得到它。只需使用glob( 'img/*.png" );

非常简单。。不要使用realpath。。