user1472277
user1472277

Reputation:

function file_exists not working in php

I have been trying to find if a file_exist in the directory. If not i want to use a different image. But as i am using the file_exists function it always returns false.

The code i used is

while($r=mysql_fetch_row($res))
    {  
        if(!file_exists('http://localhost/dropbox/lib/admin/'.$r[5]))
            {
                $file='http://localhost/dropbox/lib/admin/images/noimage.gif';
            }
        else
            $file='http://localhost/dropbox/lib/admin/'.$r[5];}

But the function always return false even if the file exits. I checked that by using

<img src="<?php echo 'http://localhost/dropbox/lib/admin/'.$r[5]; ?>" />

This displayed the image correctly.

Please Someone Help Me

Upvotes: 4

Views: 2139

Answers (6)

Toretto
Toretto

Reputation: 4711

You are passing URL to the file_exists function which is wrong. Instead of that pass your local path of the folder.

while($r=mysql_fetch_row($res))
{  
 if(!file_exists('/dropbox/lib/admin/'.$r[5]))
 {
  $file='/dropbox/lib/admin/images/noimage.gif';
 }
 else
 $file='/dropbox/lib/admin/'.$r[5];
}

Upvotes: 0

Paul Bain
Paul Bain

Reputation: 4397

The function file_exists can only work for URL protocols that are supported by the stat() function in PHP.

Currently, the http protocol is not supported by this wrapper.

https://www.php.net/manual/en/wrappers.http.php

Upvotes: 0

J.K.A.
J.K.A.

Reputation: 7404

You are passing URL to the file_exists function which is wrong parameter. Instead of that pass your local path there.

To know more about file_exist() function read this php manual :

http://php.net/manual/en/function.file-exists.php

Upvotes: 0

Mariusz Jamro
Mariusz Jamro

Reputation: 31692

file_exists() checks if file exists in local filesystem. You're passing an URL. Change it to local path to your dropbox directory and it should work:

if(file_exists('/path/to/your/dropbox'))

Upvotes: 0

lonesomeday
lonesomeday

Reputation: 238115

file_exists does not support addresses using HTTP (you can see this because stat is not included on the list of wrappers supported over HTTP). As the file_exists documentation says, remote files can be checked with some wrappers, such as FTP, but it is not possible over HTTP, so file_exists will always return false.

Presuming that this file is on the local machine (which is suggested by localhost, you'll need to access it with a local file path. It's hard to guess what this might be for you, but it might look like /var/www/dropbox....

Upvotes: 0

deceze
deceze

Reputation: 522636

file_exists uses file system paths, not URLs. You use URLs in a browser to access your PHP scripts through a web browser and web server over the network. The PHP script itself can access the local file system though and uses that, it does not go through the network stack to access files.

So use something like file_exists('C:\\foo\\bar\\dropbox\\lib\\admin\\' ...).

Upvotes: 2

Related Questions