2

I want to check exist file with php or yii2

filesize and is_file and file_exists are not working !

filesize error is : filesize(): stat failed for....
is_file and file_exists return false !

This is my code :

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

$fileI is :

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

$home is :

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

This is has correct image in view :

      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>

. . . .

childrenOurFuture
  • 1,889
  • 2
  • 14
  • 23
Saltern
  • 1,305
  • 2
  • 16
  • 42

5 Answers5

2

Try this

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

Source: link

Community
  • 1
  • 1
narasimharaosp
  • 533
  • 3
  • 12
2

If you are checking if file exist into server just you have to give the absolute path, I'm using \Yii::getAlias('@webroot') for get the absolute path and I'm executing this from a Controller.

This is my result into web browser.

Absolute Path:/home/hostinguser/public_html/files/temppdf/benja.pdf The File /home/hostinguser/public_html/files/temppdf/benja.pdf Exists

        $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";
        }
Benjamin
  • 144
  • 12
1

I have checked and verified below code and it is working fine.

$filename = '/path/to/foo.txt';

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

If you are not able to do this then the issue with your permission. Try to check file that are in desktop or some other permissible directory.

Hope this helps.

Jayesh Dhandha
  • 1,983
  • 28
  • 50
  • $filename = 'http://localhost/iicitySite/web/siteImage/parvaz.png'; if (file_exists($filename)) { echo "The file $filename exists"; } else { echo "The file $filename does not exist"; } die(); not working! The file http://localhost/iicitySite/web/siteImage/parvaz.png does not exist but this link has image when i checked that with url browser – Saltern Mar 14 '16 at 07:00
1

In you path you don't need the index.php but only the path

You can use this to get absolute(whole url including http) url :

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

If you want to use relative(relative to current path) path than you can use this :

 echo Url::to('@web/siteImage/parvaz.png', true);images/logo.png
 // http://codematrics.com/web/siteImage/parvaz.png
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
-2
$url = 'http://www.example.com'; // or file path
$headers = get_headers($url, 1);
print_r($headers);               // $headers[0] will get the status
Paul Roub
  • 36,322
  • 27
  • 84
  • 93