Page 1 of 1

A problem with large file uploads and $_FILE['file']['type']

Posted: Fri Apr 10, 2009 5:31 pm
by janusmccarthy
I'm having a problem with file upload from post. I'm setting the $_FILES variable and can successfully upload a small pdf file that is mime-type validated with following bit of code:

Where...
$ext is the extension
$this->accept is a list of known mime-types.
$this->accept[$ext] returns the mime type for the given extension.

I obtain the file and call check_type with the following:

foreach($_FILES as $file) {
if ($file['name']) { // skip empty file_0
if ($fu->check_type($file)) {

Code: Select all

 
        public function check_type($file) { // get file type
                $ext = pathinfo($file['name'],PATHINFO_EXTENSION);
                // make sure the image is a valid file type and that the type matches the extention
                if (!array_key_exists($ext,$this->accept)) {
                        $this->_error = 'invalid file type';
                        return false;
                } elseif ($this->accept[$ext] != $file['type']) {
                        $this->_error = 'file type does not match file extension.  Expecting type: '.$this->accept[$ext].'.  Found type: '.$file['type'];                        
                        return false;
                } else {
                        return true;
                }
        }
 
 
On large files (for example pdf files several MB in size) I receive an error 'file type does not match file extension. Expecting type: application/pdf. Found type: Comp615BookPart1.pdf

$file['type'] should be the file's mime-type from everything I've read. I'm not sure why I'm getting the file name here.

It's only happening on large pdf files. I've made sure php.ini has been set to handle files of that size.

I've found that the browser is responsible for filling out the parameters on the file. I am currently using Firefox 3.0.8 (latest version as of 4/10/09).

Any ideas would be greatly appreciated.

Re: A problem with large file uploads and $_FILE['file']['type']

Posted: Fri Apr 10, 2009 6:15 pm
by janusmccarthy
McInfo wrote:According to your error, this is true:

Code: Select all

$file['type'] == 'Comp615BookPart1.pdf'
That's correct.

If I do it for a small file however, $file['type'] == 'application/pdf', it's working for small pdf files, and I'm not setting the information in $_FILES (which is where I get $file from). From what I've read it's the browser that's setting the information in $_FILES.

Re: A problem with large file uploads and $_FILE['file']['type']

Posted: Fri Apr 10, 2009 6:28 pm
by janusmccarthy
McInfo wrote:I apologize for replying before I really understood the problem. Bad me.

If you upload the same PDF to this script, what do you get?

show_files.php

Code: Select all

<?php
header('Content-type: text/plain');
print_r($_FILES);
?>
Array ( [file_1] => Array ( [name] => [type] => [tmp_name] => [error] => 4 [size] => 0 ) [file_0] => Array ( [name] => Comp615BookPart1.pdf [type] => [tmp_name] => [error] => 1 [size] => 0 ) ) file

Re: A problem with large file uploads and $_FILE['file']['type']

Posted: Fri Apr 10, 2009 6:41 pm
by janusmccarthy
McInfo wrote:You are getting error #1 for file_0. That means
The uploaded file exceeds the upload_max_filesize directive in php.ini.
http://us3.php.net/manual/en/features.f ... errors.php

Your output would be easier to read if you pasted it into the code tags.
Sorry about that I forgot for that one, and the system I'm working with is a little complex, so I actually ran that code to output to a window in order to get back to you faster.

Mea culpa.

Now, I shouldn't be getting that error message, here's the grep from /etc/php.ini

Code: Select all

 
$ grep upload_max_filesize /etc/php.ini
upload_max_filesize = 10M
 
The pdf file I'm trying to upload is 6.78 MB (7,110,656 bytes).

So...I'm confuzzled unless it's grabbing the size from someplace I don't know about, and I've tried looking everywhere (it's inherited code).

Re: A problem with large file uploads and $_FILE['file']['type']

Posted: Fri Apr 10, 2009 6:55 pm
by janusmccarthy
McInfo wrote:Just to be sure, what is the output of this script?

get_umf.php

Code: Select all

<?php
echo ini_get('upload_max_filesize');
?>
Sometimes there are multiple php.ini files.
Well that seems to explain it, I'm getting 2M from within that script. Should I just do a find / -name "php.ini" or could it be under another name?

Re: A problem with large file uploads and $_FILE['file']['type']

Posted: Fri Apr 10, 2009 7:08 pm
by janusmccarthy
McInfo wrote:If you change any settings in php.ini, make sure you restart your server.
Hmm...you know, we didn't do that since we made the first change to /etc/php.ini a few days ago.

Re: A problem with large file uploads and $_FILE['file']['type']

Posted: Fri Apr 10, 2009 7:13 pm
by janusmccarthy
I'm going to have to wait til monday for the change as the admin walked out the door about 10 mins ago.