gpt4 book ai didi

PHP 在两个特定目录中搜​​索图像文件

转载 作者:行者123 更新时间:2023-12-04 04:53:47 25 4
gpt4 key购买 nike

我需要查找并回显位于这两个名为“photoA”或“photoB”的目录中的任何一个目录中的图像文件名。

这是我开始尝试通过这些目录爬行,寻找指定文件的代码:

$file = 'image.jpg';    
$dir = array(
"http://www.mydomain.com/images/photosA/",
"http://www.mydomain.com/images/photosB/"
);

foreach( $dir as $d ){
if( file_exists( $d . $file )) {
echo $d . $file;
} else {
echo "File not in either directories.";
}
}

我觉得我已经离题了。

最佳答案

您不能使用 file_exists 中的网址,您需要在服务器的文件系统中使用绝对或相对路径(相对于 runnings 脚本),例如:

        $dir = array(
"images/photosA/",
"../images/photosB/",
"/home/user/www/images/photosB/"
);

如果您不知道确切路径并在此之前添加文档根目录,您还可以使用相对于网络服务器根目录的路径:
        $dir = array(
$_SERVER['DOCUMENT_ROOT'] . "/images/photosA/",
$_SERVER['DOCUMENT_ROOT'] . "/images/photosB/"
);

(或者您使用一次,在您使用 file_exists() 的地方)

关于PHP 在两个特定目录中搜​​索图像文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17050645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com