file upload in php
I am using php upload function to upload the files in my interface. I am using firefox-3.6.11 in my browser. I am uploading excel sheets. I have tried to get the uploaded file type.I have tested the file type with following way,
$Type = $HTTP_POST_FILES['TS_FILE']['type'];
$Data = split ("/", $Type,2 ) ;
if( "$Data[1]" != "vnd.ms-excel" && $Data[1]!="octet-stream" && "$Data[1]"!="xls" && "$Data[1]" != "excel" )
{
echo "<script> //alert ( 'inside alert' ) ;
alert ( 'The selected file is not i开发者_StackOverflow社区n .xls format. Please select proper file.' ) ;
</script>";
exit;
}
It works fine. But some time I got the alert message as I mentioned in my code even if I upload the excel sheet. I don't know what is the exact problem. Is there any other excel file type is available in firefox versions???
The value of $_FILES['file']['type']
is sent by the browser, and the browser gets this value from the underlying OS. If I configure windows and tell it that .xls have mime type image/jpeg
, my browser (be it firefox, IE or whatever) would send that type to your application.
So, basically, don't rely on $_FILES['file']['type']
to do the validation. Get this value yourself server-side using php's fileinfo
:
$finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension
$type = finfo_file($finfo, $_FILES['file']['tmpname']);
Also, you can get a list of used mimetypes for excel files from this site: http://filext.com/file-extension/XLS
edit: $_FILES
instead of $_SERVER
(sorry, it's early here)
I don't know where the error is in your code as I haven't uploaded files close to that way.
I've allways used the example on W3Schools http://w3schools.com/php/php_file_upload.asp
And then edited it heavily to make it process the right kind of files (wich is where you seem to be stuck).
The way you'd want to check the file type is using $_POST['file']['type']
I see that you are indeed using this but perhaps not correctly.
You can look on W3S how they do file type checking for images, this can also be used for xls.
The way to find out what your browser sends as file type is to simply trigger_error("File type is: ".$_POST['file']['type']);
Now you can make an if statement as they used on W3S to validate the file using the file type the trigger_error reported.
Please be aware that some browsers (especially IE) could use a different name.
(sorry for not being able to give you an answer to your own script but perhaps there are some things I said that might help)
精彩评论