使用 php 检查存在文件


Check exist file with php

我想用php或yii2检查存在文件

filesizeis_filefile_exists不起作用!

filesize错误是:filesize(): stat failed for....
is_filefile_exists返回假!

这是我的代码:

 $fileI = $urlBase . "$home/../siteImage/$logo";

$fileI是:

http://localhost/iicitySite/web/index.php/../siteImage/parvaz.png

$home是:

 $home -> /iicitySite/web/index.php

这是视图中的正确图像:

      echo "

        <div class='col-lg-12 col-xs-12 col-sm-12 col-md-12 forsatItem'>
            <div class='backItem'>
                <div class='rightM col-lg-4'>
                    <div class='imgForsat'>
                        <img src='$home/../siteImage/$logo' alt=''/>
                    </div>

....

试试这个

$url = 'http://www.example.com';//or file path
$headers = get_headers($url, 1);
print_r($headers);//$headers[0] u will get the status

来源:链接

如果您正在检查服务器中是否存在文件,则必须提供绝对路径,我使用 ''Yii::getAlias('@webroot') 获取绝对路径,我正在从控制器执行此操作。

这是我在网络浏览器中的结果。

绝对路径:/home/hostinguser/public_html/files/temppdf/benja.pdf文件/home/hostinguser/public_html/files/temppdf/benja.pdf 存在

        $pathFile = 'Yii::getAlias('@webroot') .'/files/temppdf/' . 'benja.pdf'; 
        echo "<br>Absolute Path:" . $pathFile;
        if (file_exists($pathFile)) {
            echo "<br>The File $pathFile Exists";
        } else {
            echo "<br>The File $pathFile Do Not Exist";
        }

我已经检查并验证了下面的代码,它工作正常。

$filename = '/path/to/foo.txt';
if (file_exists($filename)) {
    echo "The file $filename exists";
} else {
    echo "The file $filename does not exist";
}

如果您无法执行此操作,则问题已获得您的许可。尝试检查桌面或其他允许目录中的文件。

希望这有帮助。

在路径中,您不需要索引.php而只需要路径

你可以用它来获取绝对(整个网址包括http)网址:

 echo Url::to('@web/siteImage/parvaz.png', true);
 // http://codematrics.com/web/siteImage/parvaz.png

如果要使用相对(相对于当前路径)路径,则可以使用:

 echo Url::to('@web/siteImage/parvaz.png', true);images/logo.png
 // http://codematrics.com/web/siteImage/parvaz.png
$url = 'http://www.example.com'; // or file path
$headers = get_headers($url, 1);
print_r($headers);               // $headers[0] will get the status