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> 

. . . .

3
  • Is index.php an actual folder name on your server? Commented Mar 14, 2016 at 6:44
  • Might be you have to give permission to DIR where file stored. Please try it. Commented Mar 14, 2016 at 6:44
  • use echo Yii::getAlias('@web').'/directoryName/ImageName'; Commented Mar 14, 2016 at 6:57

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

Comments

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"; } 

Comments

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.

1 Comment

$filename = '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 localhost/iicitySite/web/siteImage/parvaz.png does not exist but this link has image when i checked that with url browser
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 

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.