【问题标题】:PHP if file existsPHP 如果文件存在
【发布时间】:2013-02-11 00:17:54
【问题描述】:

这是我的代码。

$filename = "$myMedia catalog/category/ $myImage.png";
$filename = str_replace(" ", "", $filename);

if (file_exists($filename)) {
    echo "The file $filename exists";
} else {
    echo "The file $filename does not exist";
}

上面的variable 输出确实存在的http://50.87.6.244/~storeupp/media/catalog/category/Game_Used.png。但是,它说它不存在。

知道为什么吗?

【问题讨论】:

标签: php


【解决方案1】:

试着像这样使用:

 $filename = dirname(__FILE__) . "/ $myMedia catalog/category/ $myImage.png";
 $filename = str_replace(" ", "", $filename);

 if (file_exists($filename)) {
     echo "The file $filename exists";
 } else {
     echo "The file $filename does not exist";
 }

【讨论】:

  • 非常感谢!是的,我只需要删除 http:// 并且只包含目录 url。谢谢!将在 7 分钟内接受 :) +投票
【解决方案2】:

我不知道,但我认为,如果您将外部 Web 项目作为资产,以下代码会更成功:

$filename = $myMedia."catalog/category/".$myImage.".png";
$headers = @get_headers($filename);
$exists = ($file_headers[0] == 'HTTP/1.1 404 Not Found');

【讨论】:

    【解决方案3】:

    OT:您可以将变量括在“{}”括号之间,而不是使用空格和 str_replace(" ", "", $filename)。

    $filename = "{$myMedia}catalog/category/{$myImage}.png";

    【讨论】:

      【解决方案4】:

      该文件看起来在您的网络根目录之外。因此,它无法通过网络获得。如果您尝试检查计算机上的本地文件,请改用文件根目录。 (即 /path/to/media/catalog/category/Game_Used.png)

      【讨论】:

        【解决方案5】:

        你需要引用文件的路径或者你不能使用的目录http://链接

        file_exists documentation

        【讨论】:

          【解决方案6】:

          documentation 中声明 file_exists() 函数仅适用于 PHP 5.0 的某些 URL 包装器。

          【讨论】:

            猜你喜欢
            • 2014-08-09
            • 1970-01-01
            • 1970-01-01
            • 2015-03-18
            • 2013-08-10
            • 2017-02-20
            • 2013-11-05
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多