Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP's file_exists() will not work for me?

For some reason this PHP code below will not work, I can not figure it out.

It is very strange, file_exists does not seem to see that the image does exist, I have checked to make sure a good file path is being inserted into the file_exists function and it is still acting up

If I change file_exists to !file_exists it will return an images that exist and ones that do not exist

define('SITE_PATH2', 'http://localhost/');  $noimg = SITE_PATH2. 'images/userphoto/noimagesmall.jpg'; $thumb_name = 'http://localhost/images/userphoto/1/2/2/59874a886a0356abc1_thumb9.jpg'; if (file_exists($thumb_name)) {     $img_name = $thumb_name; }else{     $img_name = $noimg; } echo $img_name; 
like image 485
JasonDavis Avatar asked Aug 17 '09 13:08

JasonDavis


People also ask

How do you check is file exist in PHP?

The file_exists() function checks whether a file or directory exists.

How can check file in folder in PHP?

The file_exists() function in PHP is an inbuilt function which is used to check whether a file or directory exists or not. The path of the file or directory you want to check is passed as a parameter to the file_exists() function which returns True on success and False on failure.

How create file if not exist in PHP?

PHP Create File - fopen() The fopen() function is also used to create a file. Maybe a little confusing, but in PHP, a file is created using the same function used to open files. If you use fopen() on a file that does not exist, it will create it, given that the file is opened for writing (w) or appending (a).

How do you check whether a file exists or not?

To check if a file exists, you pass the file path to the exists() function from the os. path standard library. If the file exists, the exists() function returns True . Otherwise, it returns False .


1 Answers

file_exists() needs to use a file path on the hard drive, not a URL. So you should have something more like:

$thumb_name = $_SERVER['DOCUMENT_ROOT'] . 'images/userphoto/1/2/2/59874a886a0356abc1_thumb9.jpg'; if(file_exists($thumb_name)) {     some_code } 

http://us2.php.net/file_exists

like image 87
AvatarKava Avatar answered Oct 01 '22 09:10

AvatarKava