1

I need to be able to tell if an image exists in a directory or not. Given the file name and the directory, how can I tell if it exists?

Thanks!

JD Isaacks
  • 56,088
  • 93
  • 276
  • 422

6 Answers6

5
file_exists($filename);

http://www.php.net/file_exists

stefs
  • 18,341
  • 6
  • 40
  • 47
  • 3
    It's important to note that `file_exists` will return true for a directory as well as a file; if you also want to make sure that it's really a file, you want `is_file` instead. – Ben Blank May 20 '09 at 13:54
4
$dir = '/var/img/'; $name = 'img.jpg';

echo is_file($dir.$name);
powtac
  • 40,542
  • 28
  • 115
  • 170
1
bool file_exists(string $filename)
TStamper
  • 30,098
  • 10
  • 66
  • 73
1

If you need to know more than file_exists() you should look at the stat function... It can tell you if a file exists and if so how big it is, and what type of file it is (and about a dozen other things)...

dicroce
  • 45,396
  • 28
  • 101
  • 140
1
<?php
$filename = '/path/to/foo.txt';

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

Source: http://in.php.net/file_exists

NinethSense
  • 8,824
  • 2
  • 23
  • 23
0

You are talking about image... possibly you try to find a way to put some "no-picture" image instead of nonexistent?

If yes - look at something like this. Else read the manual as people said before...

Jet
  • 1,171
  • 6
  • 8