Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check if file is right type, and the file has right extension.

Status
Not open for further replies.

ReOja

Instructor
Dec 17, 2007
2
FI
Hello.

I should check if the uploaded file has right extension,
and type.

I can get the filetype by File::Type module
Code:
my $type = $ft->checktype_filename($file);
So now the type could look like this:
image/x-png
How can I get the first word (image)of that line?

If I could get that word, then I would be able to check is the uploaded file really a image.

Second question:
Does anybody a good method how I could read/check the file extension? I allow only these extensions: (jpg, gif, or png).



 
Code:
($filetype, $fileformat)=split (/\//, $type);
if ($filetype eq 'image') {
 #we got an image
}

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
You could use Paul's technique to get the extension as well - simply split on a period rather than a forwardslash.

Yours,

fish

["]As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life was going to be spent in finding mistakes in my own programs.["]
--Maurice Wilkes, 1949
 
File::Basename (core module) can be used to check file names/paths/extensions and to filter wanted/unwanted file extensions. See the File::Basename documentation for details.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thank you guys. I got everything what I needed!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top