Checking if a file is an image file.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Checking if a file is an image file.

Post by JellyFish »

How do I check if an URI is a image file type?

I'm "scanning" through a directory and want to do somethings with only the files that are images.

How could I go about this?

Please help me with one. :(

Thanks for reading.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

You might be interested in http://de2.php.net/fileinfo
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

How do I use these? I don't have the set of functions apparently cause I get the error:
Fatal error: Call to undefined function: finfo_file() in /home/content/t/r/a/tradingtresure/html/manager/files.php on line 51
Does anyone have any other solutions?
User avatar
maliskoleather
Forum Contributor
Posts: 155
Joined: Tue May 15, 2007 2:19 am
Contact:

Post by maliskoleather »

its basically the same as fopen/fread

Code: Select all

$finfo = finfo_open(FILEINFO_MIME);
echo finfo_file($finfo, $filename) . "\n";
finfo_close($finfo);
you need to install it though. the page linked above has the link and instructions to do so.

alternatively, you could use the stat() or mime_content_type() functions, though neither are as clean. fileinfo executes the process better.
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

Code: Select all

foreach(glob("*.{jpg,png,gif}", GLOB_BRACE) as $fname)
	if(getimagesize($fname))
		// $fname is an image
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

maliskoleather wrote:its basically the same as fopen/fread

Code: Select all

$finfo = finfo_open(FILEINFO_MIME);
echo finfo_file($finfo, $filename) . "\n";
finfo_close($finfo);
you need to install it though. the page linked above has the link and instructions to do so.

alternatively, you could use the stat() or mime_content_type() functions, though neither are as clean. fileinfo executes the process better.
Am I allowed to install php extensions on a shared(I believe shared) hosting server, like godaddy?

Why would "they" depreciate mime_content_type() when they don't include the fileinfo extensions by default with PHP 5?
Last edited by JellyFish on Wed May 23, 2007 10:10 pm, edited 1 time in total.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

probably not, but maybe getimagesize() is available.
see http://de2.php.net/getimagesize
User avatar
maliskoleather
Forum Contributor
Posts: 155
Joined: Tue May 15, 2007 2:19 am
Contact:

Post by maliskoleather »

JellyFish wrote: Am I allowed to install php extensions on a shared(I believe shared) hosting server, like godaddy?
no. you could probably send them and email and ask for it. most hosts are glad to install extensions for users.
JellyFish wrote: Why would "they" depreciate mime_content_type() when they don't include the fileinfo extensions by default with PHP 5?
because mime_content_type() is clunky, slow, and more work on the server than nessicary.
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

Would installing extensions take more space on their server or make pages run slower?

What does glob() do, i didn't quite understand on php.net? And what's glob stand for?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Arbitrary modules can execute arbitrary code. The provider might not like that.
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

I see.

I'll contact them then.

But if they install the extension, will the extension be available for all other hosting customers or just for my account?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Depends on how the extension is installed.
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

They'll most likely install to my local directory access right?

Or would they actually install it for everyone? Have they(hosting services) ever done that?
User avatar
maliskoleather
Forum Contributor
Posts: 155
Joined: Tue May 15, 2007 2:19 am
Contact:

Post by maliskoleather »

depends on how they have php set up and configured.
Some hosts will install it for one user, some hosts will install it for everyone on that server.
It just depends.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Just ask them. In the meanwhile getimagesize() will probably suffice.
Post Reply