Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-如何访问文件的正确路径?_Php_Url_Path_Filepath_File Exists - Fatal编程技术网

PHP-如何访问文件的正确路径?

PHP-如何访问文件的正确路径?,php,url,path,filepath,file-exists,Php,Url,Path,Filepath,File Exists,我尝试使用第一种和第二种方法来查找文件的路径,并验证文件是否存在 文件\u存在()。第一个不起作用,因为文件\u exists()不接受相对路径,第二个返回错误的不存在路径:http://www.kvetena.local/data/www/kvetena.local/web/uploadedimages/project-4-0 正确的路径是:Z:\kvetena.local\web\uploadedimages\,但它将在将来更改。如何访问该文件夹中的文件?用于获取绝对路径: $filenam

我尝试使用第一种和第二种方法来查找文件的路径,并验证文件是否存在
文件\u存在()
。第一个不起作用,因为
文件\u exists()
不接受相对路径,第二个返回错误的不存在路径:
http://www.kvetena.local/data/www/kvetena.local/web/uploadedimages/project-4-0

正确的路径是:
Z:\kvetena.local\web\uploadedimages\
,但它将在将来更改。如何访问该文件夹中的文件?

用于获取绝对路径:

$filename='../uploadedimages/project-'.$item->id.'-'.$imgId;
$filename=$_SERVER['DOCUMENT_ROOT'].'/uploadedimages/project-'.$item->id.'-'.$imgId;

如果您将只使用图像,我可以建议您另一种方法来检查文件是否存在

$absPath = realpath($filenamed);

您可以将此函数与相对路径一起使用

$filename='../uploadedimages/project-'.$item->id.-'.$imgId;回声“;这不起作用-返回空“”路径它可能返回空字符串,因为
realpath
返回了
false
。阅读有关此案例的手册部分:可能是因为文件不存在,或者您运行脚本的用户没有权限。它不起作用,返回false并带有“../uploadedimages/project-”。$item->id.-”。$imgId;路径,即使文件存在
function file_exists_url($url){
        if(@GetImageSize($url)) return true;
        else return false;
    }