Php file_exists 无效

Php file_exists does not work

我想检查目录中的文件是否存在。 我的情况确实存在。

if(file_exists("C:\xampp\htdocs\myApp\img1.jpg"))
{
    echo "is file";
    exit;
}

这是该路径的图片:

但不知何故这不起作用。

正斜杠也适用于 Windows。您可以使用正斜杠,这样您就不需要对它们进行转义:

if (file_exists("C:/xampp/htdocs/myApp/img1.jpg"))
{
    echo "file exists";
    exit;
}

请仅使用一个 \ 并按照以下说明操作:

if(file_exists("C:\xampp\htdocs\myApp\img1.jpg"))
{
    echo "is file";
    exit;
}

if(file_exists("img1.jpg"))
{
    echo "is file";
    exit;
}

因为 img1.jpg 是您的项目根文件夹