0

I've been scouring the Code Igniter forums for the past hour trying to figure this one out:

I'm writing a file upload handler for a web application using Code Igniter. I have the following code to handle the upload so far:

public function send() {        
    $config = array(
        'upload_path' => 'path/to/my/upload/directory',
        'allowed_types' => 'pdf'
    );

    $this->load->library('upload', $config);

    $this->upload->do_upload('pdf_upload');

    echo "<pre>";
    print_r($this->upload->data());
    echo "</pre>";
    exit();
}

And my view:

<?= $errors ?> <br />
<?= form_open_multipart('/Upload_test/send') ?>
    <p><label for="pdf_upload">File: (PDF ONLY)</label> <input type="file" name="pdf_upload" id="pdf_upload" /></p>
    <p><input type="submit" /></p>
</form>

When I submit the form with a valid PDF file selected, I get the following output from print_r():

Array
(
    [file_name] => my_file.pdf
    [file_type] => 
    [file_path] => path/to/my/upload/directory/
    [full_path] => path/to/my/upload/directory/my_file.pdf
    [raw_name] => my_file
    [orig_name] => 
    [client_name] => my_file.pdf
    [file_ext] => .pdf
    [file_size] => 4190
    [is_image] => 
    [image_width] => 
    [image_height] => 
    [image_type] => 
    [image_size_str] => 
)

The file type is blank. What could be causing this? What am I missing?

Matt
  • 6,993
  • 4
  • 29
  • 50
  • What version of CI are you running? – nageeb Sep 05 '12 at 15:58
  • I see you found the answer. I was going to suggest that other question but wasn't sure if it was applicable. – nageeb Sep 05 '12 at 16:02
  • @nageeb yeah, I wasn't sure it was a duplicate question. Clearly dangermark (the asker of that question) was getting a file type when he uploaded; I wasn't. Happily, the answer to his question was also the answer to mine. :-) – Matt Sep 05 '12 at 16:08

1 Answers1

0

I found the answer in a related, but not duplicate question: Uploading in Codeigniter - The filetype you are attempting to upload is not allowed

If you're using Codeigniter version 2.1.0 there is a bug in the Upload library. See http://codeigniter.com/forums/viewthread/204725/ for more details.

Basically what I did was modify a few lines of code in the File Upload Class (Location: ./system/libraries/Upload.php)

1) modify Line number 1044

from:

$this->file_type = @mime_content_type($file['tmp_name']);
return;

to this:

$this->file_type = @mime_content_type($file['tmp_name']);
if (strlen($this->file_type) > 0) return; 

2) modify line number 1058

from:

@exec('file --brief --mime-type ' . escapeshellarg($file['tmp_path']), $output, $return_code);

to this:

@exec('file --brief --mime-type ' . escapeshellarg($file['tmp_name']), $output, $return_code); 

As you can probably see, line 1058 tries to use an array value that does not exist.

Community
  • 1
  • 1
Matt
  • 6,993
  • 4
  • 29
  • 50
  • 1
    Glad you fixed it, but you should extend the core library instead of editing it directly. Just copy the methods you edited from the core library into the new one and that way your changes won't break if you update the CI core. – Brendan Sep 05 '12 at 19:57