Welcome Guest, Not a member yet? Register   Sign In
File Upload library issues (again)
#1

[eluser]captainredmuff[/eluser]
Hi all,

Not sure if this is the right place to post, but here goes.

I know there are quite literally hundreds of posts regarding file upload issues with the File Upload library (i have read many of these which has lead me to write this post) but maybe someone can help me out with this one.

Seeing that there is no simple way to allow all file types to be uploaded, i have compiled an array of file types i would like to accept via upload which is then imploded into a string a la:

Code:
//array of allowed file types
$file_types = array("zip" => "Archive", "pdf" => "Document", "gif" => "Image"); //etc etc

//array of config vars for file upload library
$config = array();

//compile allowed file types
$config['allowed_types'] = implode("|", array_keys($file_types));

When i upload either a zip or a pdf, the file upload works fine but simply wont accept any form of image be it a gif, jpg or png (obviously, i include these types in the above array).

If i move the image types to the beginning of the array, i get the adverse effect of only being able to upload images and all other file types are rejected.

The logic behind this confuses me immensly and i've just wasted an entire morning trying to rectify this issue. Why should it make any difference where abouts in the array the image formats are defined?

From looking through the File Upload library, i can see that the vast majority of validation is done with a simple in_array() check. I have modified the library by means of including debugging comments to allow me to trace the issue and it seems that everything grinds to a halt when CI checks to see if the file in question is a gif. This seems to stem from the implementation of the is_allowed_filetype() method inside the File Upload library. I did see a post mentioning overloading the File Upload library but then my shoddy damn winblows office pc perished with a BSOD and i have no idea where to find it again.

From trawling around previously, it seems that a lot of people have this issue. why hasn't this been patched? Is there a working fix or any method to circumvent this? Having had to patch the Session library yesterday to deal with the short comings of IE and damn cookies, i'm starting to feel a little worn down by the prevelant issues with CI that seem to rear their ugly head every time a project is close to completion.

Any help/info/push in the right direction would be greatly appreciated.
#2

[eluser]Kow[/eluser]
I've just solved a similar problem: http://ellislab.com/forums/viewthread/122992/
#3

[eluser]captainredmuff[/eluser]
Thanks for your reply, i posted a response.

Unfortunatly i won't be implementing this "fix".
#4

[eluser]Tobz[/eluser]
Ok So I've been having issue with Firefox sending invalid mime-types for ages.
I finally found this post (<a href="http://techblog.procurios.nl/k/news/view/15872/14863/Mimetype-corruption-in-Firefox.html">http://techblog.procurios.nl/k/news/view/15872/14863/Mimetype-corruption-in-Firefox.html</a>) that explains exactly why.

So I made these changes to the upload library via application/libraries/MY_upload.php

Code:
function is_allowed_filetype() {
    if (count($this->allowed_types) == 0 OR !is_array($this->allowed_types)) {
      $this->set_error('upload_no_file_types');
      return FALSE;
    }
    
    // reset mime based on what the server thinks is correct.
    // This is to fix Firefox's sometimes incorrect mime
    $file_mime = false;
    if (function_exists('finfo_file')) {
      $finfo = @finfo_open(FILEINFO_MIME);
      $file_mime = @finfo_file($finfo, $this->file_temp);
      @finfo_close($finfo);
    }
    elseif (function_exists('mime_content_type')) {
      $file_mime = @mime_content_type($this->file_temp);
    }
    
    if ($file_mime) {
      $this->file_type = strtolower($file_mime);
    }
    // end mime fix

    ...
EDIT: I just added a fallback using mime_content_type(depricated) for servers that don't have finfo_file.

So far this has worked for me - I haven't tested thoroughly though.
I hope this helps someone else - cos damn its been a pain in the 4rse.

Chur,
Tobz




Theme © iAndrew 2016 - Forum software by © MyBB