• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem uploading PDF Files

The problem with PDF seems to be the preg_replace function in the Uploads.php Library.

if you echo out the $_FILES['userfile']['type'] you will get


The String is escaped! So this doesn't match anything int he mime type class. In the do_upload() method in the Uploads Class you can see there is code that is supposed to do a regular expression replace

$this->file_type = preg_replace("/^(.+?);.*$/", "\\1", $_FILES[$field]['type']);

This expression isn't working correctly. What I have done as a quick fix is to just do a str_replace() on the '\"' combination. just comment out the preg_replace and add this.

$this->file_type = str_replace('\"', '', $_FILES[$field]['type']);

Hope this works for you. Will post update for preg_replace() later.

I've tried every fix I can find for this problem and nothing seems to work. I've tried on both Safari 4 and FF 3.5. I'm on a Mac so IE isn't an option. Does anyone have any other suggestions on how I can get this to work? I really need to be able to upload PDF files. Thanks. Below is my code - maybe some fresh eyes can see a problem.

The file type being listed by $_FILES[$field_name]['type'] is application/pdf with no quotes or commenting out.

function _pdfUpload(){
        $config['upload_path'] = './images/frontpages/';
        $config['allowed_types'] = 'pdf';
        $config['max_size'] = '1000';
        $this->load->library('upload', $config);
        $field_name = 'pdf';
        if (!$this->upload->do_upload($field_name)){
        } else {
            $data = $this->upload->data();
            return $data;

In my mimes file this is the line for PDF:
'pdf'    =>    array('application/pdf', 'application/x-pdf', 'application/x-download','application/download','binary/octet-stream'),

Not sure if this is a bug or not. But is solved my problem.

It looks like in Upload.php the following code is used to test if the file is an image. I don't have great knowledge of how image files work but I was having this return FALSE on a .pdf and a .php file. After doing a print_r(getimagesize($this->file_temp)); I determine that both these files were being read as images in getimagesize. I just commented out that chunk of code and kept going.

One Line 566ish in Upload.php

if (in_array($val, $image_types))
                if (getimagesize($this->file_temp) === FALSE)
                    return FALSE;

To follow that up. Here is two print_r's from

$data = array('upload_data' => $this->upload->data());

After I completed the upload. As you can see it thinks the file has image data.

    [upload_data] => Array
            [file_name] => test_parse_addresses.php
            [file_type] => application/octet-stream
            [file_path] => /tmp/cloudcrm_1255228219_1340206574/
            [full_path] => /tmp/cloudcrm_1255228219_1340206574/test_parse_addresses.php
            [raw_name] => test_parse_addresses
            [orig_name] => test_parse_addresses.php
            [file_ext] => .php
            [file_size] => 1.38
            [is_image] =>
            [image_width] => 590
            [image_height] => 480
            [image_type] => gif
            [image_size_str] => width="590" height="480"


    [upload_data] => Array
            [file_name] => moveout.pdf
            [file_type] => application/octet-stream
            [file_path] => /tmp/cloudcrm_1255228219_1340206574/
            [full_path] => /tmp/cloudcrm_1255228219_1340206574/moveout.pdf
            [raw_name] => moveout
            [orig_name] => moveout.pdf
            [file_ext] => .pdf
            [file_size] => 84.25
            [is_image] =>
            [image_width] => 191
            [image_height] => 264
            [image_type] => jpeg
            [image_size_str] => width="191" height="264"


For people that read this forum with (about) the same problem: there is a bug in Upload class (CI 1.7.2) that can cause this error, see: http://codeigniter.com/bug_tracker/bug/6780/

Quick solution without patching CI is to set non-image types in front of allowed_types. So instead of "jpg|gif|pdf|zip", you should use "pdf|zip|jpg|zip".

Interesting. Thanks!!!

I had to do the following:

'pdf' => array('application/pdf', 'application/x-pdf', 'text/html'),

Editing the config/mimes.php seems to be the best way to get rid of this problem.

Keep security in mind though...

print_r($this->upload->data()); and then see if the file_type exists for the according extension in mimes.php. If not, just add it and it will work. Putting the image types don't work for me (I allow a large set of types), and hacking the Upload.php class doesn't work as well.

[eluser]marcello romani[/eluser]
Hallo everybody,
I have the same problem: I cannot upload PDF files because the mime type is not allowed even though I have it in allowed_types:

$upload_config['allowed_types'] = 'pdf|txt|jpg|png';

I see the problem with FF 3.5.7 on Ubuntu 9.10 (the exact user agent string is "Mozilla/5.0 (X11; U; Linux i686; it; rv: Gecko/20100106 Ubuntu/9.10 (karmic) Firefox/3.5.7").
However, I can upload pdfs just fine with IE 7 on Windows XP SP3 (exact IE version is 7.0.5730.13).

I have used Wireshark and I found something interesting: FF sends the file as text/html, while IE sends the same file as application/pdf. So it seems it's not a CI bug, but rather a FF one!

Partial dump of FF session:

Quote:POST /~marcello/php/ci_doctrine_upload/index.php/upload/submit HTTP/1.1

Host: localhost
User-Agent: Mozilla/5.0 (X11; U; Linux i686; it; rv: Gecko/20091215 Ubuntu/9.10 (karmic) Firefox/3.5.6
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: it-it,it;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: http://localhost/~marcello/php/ci_doctrine_upload/index.php/upload/submit
Content-Type: multipart/form-data; boundary=---------------------------1078459809665128897602238684
Content-Length: 291301

Content-Disposition: form-data; name="filename"; filename="sigmetrics09.pdf"
Content-Type: text/html

5 0 obj
<</Length 6 0 R/Filter /FlateDecode>>
x..}[....f.>j.#............k....=.......$.DjE.CI<[email protected]:..SQ]@..D...d}.d]....../.~.>.-....7.(..I....._>.....'*,........V.I

Partial dump of IE session:

Quote:POST /~marcello/php/ci_doctrine_upload/index.php/upload/submit HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, */*
Accept-Language: it
Content-Type: multipart/form-data; boundary=---------------------------7dac53100ce
UA-CPU: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)
Content-Length: 291247
Connection: Keep-Alive
Cache-Control: no-cache
Cookie: ci_session=a:4:{s:10:"session_id";s:32:"22efd9ab2e26f4965f85661ffb90b609";s:10:"ip_address";s:9:"";s:10:"user_agent";s:50:"Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+5.1)";s:13:"last_activity";s:10:"1262950328";}3fd09c2646963923d9cc0436b498aa30

Content-Disposition: form-data; name="filename"; filename="Z:\home\sigmetrics09.pdf"
Content-Type: application/pdf

5 0 obj
<</Length 6 0 R/Filter /FlateDecode>>
x..}[....f.>j.#............k....=.......$.DjE.CI<[email protected]:..SQ]@..D...d}.d]....../.~.>.-....7.

[eluser]marcello romani[/eluser]
[quote author="tison" date="1258175261"]I had to do the following:

'pdf' => array('application/pdf', 'application/x-pdf', 'text/html'),

Despite my previous findings, this workaround didn't work for me. Instead I had to do:

'pdf' => array('application/pdf', 'application/x-pdf', 'application/force-download')

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.