【问题标题】:Php file_exists does not workphp file_exists 不起作用
【发布时间】:2025-12-09 20:00:01
【问题描述】:

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

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

这是该路径的图像:

但不知何故,这不起作用。

【问题讨论】:

  • windows 想要在驱动器号后面有 两个 退格。并且由于您必须转义它们,因此您必须在 C: 之后写 四个 退格
  • "在 Windows 上,使用 //computername/share/filename 或 \\computername\share\filename 检查网络共享上的文件。"
  • @kosta 这是一个本地驱动器。不是网络共享
  • 这将起作用if(file_exists("C://xampp/htdocs/myApp/img1.jpg"))
  • 路径也可以使用正斜杠。

标签: php file path exists


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

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

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

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

    【讨论】:

    • 你能解释一下你的项目文件夹/文件结构吗?