how to check a file path is an image or not in php?
Question
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.......
Solution
Use the file info functions.
OTHER TIPS
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.
You need to check their extentions like this:
if (strpos($File_Paths, 'jpg') !== false || strpos($File_Paths, 'gif') !== false)
{
// yes there is an image.
}
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
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow