tags:

views:

112

answers:

4

Hi guys,

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.......

+1  A: 

Use the file info functions.

Ignacio Vazquez-Abrams
@Ignacio any sample
udaya
Other than the examples at that link?
Ignacio Vazquez-Abrams
@Ignacio, are file_info functions your typical choice in determining image types? I see MIME type is deprecated.
rrrfusco
@rrrfusco: `mime_content_type()` is deprecated. The other functions are not.
Ignacio Vazquez-Abrams
A: 

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
A: 

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
+1  A: 

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 G. Vicario