Forum Moderators: coopster

Message Too Old, No Replies

php: file not exist routine

         

scorpion

5:11 pm on Mar 31, 2003 (gmt 0)

10+ Year Member


If you are opening an http resource such as an image, e.g. http://www.asite.com/animage.jpg and animage.jpg does not exist, but the site does an automatic redirect to a "file not found page", how can your PHP script detect this as an "image file not found"?

toadhall

6:40 pm on Mar 31, 2003 (gmt 0)

10+ Year Member



Use fopen() with a read (r).

See the third User Contributed Note here [php.net].

file_exists() works only on local files.

T

scorpion

7:44 pm on Mar 31, 2003 (gmt 0)

10+ Year Member



Thanks, but this does not work. I've tested it, it will still return TRUE if ANY file is opened successfully, in this case it would be because if the JPEG is not found, it redirects to an HTML page which will be "true". My question is if there is a script that can distinguish that an actual image (gif or jpg) was opened/retrieved as opposed to an HTML or text file...

Fischerlaender

8:29 pm on Mar 31, 2003 (gmt 0)

10+ Year Member



I'd use some lower-level functions to request the file via http and check the response header for "200 OK" and the appropriate Content-Type.

toadhall

9:02 pm on Mar 31, 2003 (gmt 0)

10+ Year Member



How about this?

<?
$file = @file(path/to/file);
echo ("File " . ($file? "exists<br>" : "does not exist<br>"));
?>

T