By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,664 Members | 1,643 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,664 IT Pros & Developers. It's quick & easy.

Check the file extension

100+
P: 190
Hi,

I want to check the extension of uploaded file.

I upload the bmp image using file property. Now i am going to edit the file upload text as bmp file into gif...

example : i upload c:/sample.bmp now i manually edit the file input box as c:/sample.gif and i upload this one.

Now i want to check the orignial file ext.. is there any way in javascript or php.....
Sep 20 '08 #1
Share this Question
Share on Google+
10 Replies


Atli
Expert 5K+
P: 5,058
Hi.

What do you mean by: "manually edit the file input box"?
What are you doing with the files after you upload them?

You need to explain this a lot better.
Sep 20 '08 #2

pbmods
Expert 5K+
P: 5,821
Heya, maheswaran.

Instead of validating the extension, try validating the mime-type of the file itself. The Fileinfo extension works very well for this (http://php.net/finfo_file).
Sep 20 '08 #3

chelvan
P: 90
hi

use the following for your if condition

$_FILES["file"]["type"];



regards

chel-1
Sep 22 '08 #4

Atli
Expert 5K+
P: 5,058
hi

use the following for your if condition

$_FILES["file"]["type"];



regards

chel-1
It's not good to rely on that tho. This is supplied by the client, so it can be manipulated.

From the manual:
$_FILES['userfile']['type']
The mime type of the file, if the browser provided this information. An example would be "image/gif". This mime type is however not checked on the PHP side and therefore don't take its value for granted.
Sep 22 '08 #5

100+
P: 190
Hi Atli,

Manually do means..

First i upload a file...

Now input box have <input type="file" name="uploadto" value="c:/sample.gif">

Then am going to change the value gif as jpg <input type="file" name="uploadto" value="c:/sample.jp..."


After change am going to upload..so am have error... In that case i want to check the file extension or file validation....
Sep 22 '08 #6

chelvan
P: 90
It's not good to rely on that tho. This is supplied by the client, so it can be manipulated.

From the manual:
hi
thanks for your information.

find out the "/" position then using substr() the user can check the extension. this is the way i thought.

so......... the above is wrong no?


thanks
chel-1
Sep 22 '08 #7

Atli
Expert 5K+
P: 5,058
Hi Atli,

Manually do means..

First i upload a file...

Now input box have <input type="file" name="uploadto" value="c:/sample.gif">

Then am going to change the value gif as jpg <input type="file" name="uploadto" value="c:/sample.jp..."
How do you upload that first file?

Setting the "value" attribute on a <input type="file"> element should have no effect. It will simply be ignored. You will actually have to hit the "browse" button and manually select a file before submitting the form.

After change am going to upload..so am have error... In that case i want to check the file extension or file validation....
What error are you getting?
Can we see the code you are using?

It's very hard to understand the problem without the code, especially with such a limited description of what it is you are doing.
Sep 22 '08 #8

Atli
Expert 5K+
P: 5,058
hi
thanks for your information.

find out the "/" position then using substr() the user can check the extension. this is the way i thought.

so......... the above is wrong no?


thanks
chel-1
The file extension is also a very unreliable way to determine the type of a file.
I could simply add a ".jpeg" extension to any file and your script would accept it as a JPEG image.

Using the method suggested by pbmods is much more reliable. It actually checks the file, not just the file name or the data feed to you by the client.
Sep 22 '08 #9

chelvan
P: 90
The file extension is also a very unreliable way to determine the type of a file.
I could simply add a ".jpeg" extension to any file and your script would accept it as a JPEG image.

Using the method suggested by pbmods is much more reliable. It actually checks the file, not just the file name or the data feed to you by the client.
hi atli !
thanks for your information
chel-1
Sep 22 '08 #10

100+
P: 190
yes pbmods code is seems to be fine.Thanks
Sep 22 '08 #11

Post your reply

Sign in to post your reply or Sign up for a free account.