3

I have a table that stores file paths of images.documents,pdf etc... My query is

Select File_Paths from Uploads

Now how do I check whether the file path is image or not using PHP... If it is an image i have to view it or else download it.......

Jacob Relkin
  • 161,348
  • 33
  • 346
  • 320
udaya
  • 9,598
  • 15
  • 48
  • 67
  • 4
    See this related questions and it's answers to learn how to determine the MimeType of a file: http://stackoverflow.com/questions/2006632/php-how-can-i-check-if-a-file-is-mp3-or-image-file – Gordon Jan 22 '10 at 13:25

4 Answers4

3

Good old getimagesize() is a reasonable way to find out whether a file contains a valid image. You just need to test its return value against FALSE:

<?php

$is_picture = getimagesize($filename)!==FALSE;

?>

Of course, it's a good idea to do it only once and store the result into the database.

If this is not what you're looking for, please clarify.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • 1
    Worth noting that getimagesize() will throw exceptions if the file is not readable, so even better is to do: `$is_picture = is_file($filename) && is_readable($filename) && getimagesize($filename)` – mrbellek Jan 30 '13 at 09:02
2

Use the file info functions.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
1

You need to check their extentions like this:

if (strpos($File_Paths, 'jpg') !== false || strpos($File_Paths, 'gif') !== false)
{
  // yes there is an image.
}
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
0

You can chack extensions of the paths from the query. For example(using MySQL):

$result = mysql_query('Select File_Paths from Uploads');
$extArray = array('gif', 'jpg', 'jpeg', 'png');
while ($row=mysql_fetch_row($result)) {
  $extension = substr($row[0], strrpos($row[0], ".")+1);
  if (in_array($extension, $extArray)) {
    // Do something with image
  }
  else {
    // Download other files
  }
}
Alex
  • 3,140
  • 2
  • 22
  • 23