33

I'm experiencing a very odd upload problem. Here's the relevant view file:

<form action="http://localhost/index.php/temp/upload/" method="post" enctype="multipart/form-data">
    <fieldset>
        <input type="file" name="userfile"/>
        <input type="submit" value="Upload"/>
    </fieldset>
</form>

And here's my temp controller's upload() method:

public function upload()
{
    $config['upload_path']   = FCPATH . 'uploads' . DIRECTORY_SEPARATOR;
    assert(file_exists($config['upload_path']) === TRUE);
    $config['allowed_types'] = 'avi|mpg|mpeg|wmv|jpg';
    $config['max_size']      = '0';

    $this->load->library('upload', $config);
    if ($this->upload->do_upload('userfile') === FALSE)
    {
        // Some error occured
        var_dump($this->upload->display_errors('', ''));
        var_dump($_FILES);
    }
    else
    {
        // Upload successful
        var_dump($this->upload->data());
    }
}

When I upload an AVI video, everything works fine. When I upload, say, a WMV video, I get the following var dumps:

string 'The filetype you are attempting to upload is not allowed.' (length=57)

array
  'userfile' => 
    array
      'name' => string 'wmv.wmv' (length=7)
      'type' => string 'video/x-ms-wmv' (length=14)
      'tmp_name' => string 'C:\wamp\tmp\php2333.tmp' (length=23)
      'error' => int 0
      'size' => int 83914

The "wmv" extension is being interpreted as the MIME type: video/x-ms-wmv. This should be fine since my config/mimes.php has the following:

'wmv' =>  array('video/x-ms-wmv', 'audio/x-ms-wmv')

It's a similar situation when I try uploading other files. So far, the only one that seems to work is my test AVI video.

Any ideas what might be wrong?

UPDATE 1:

One my machine, only AVI uploads. On another developer's machine, no files upload. On yet another developer's machine, all supported files upload. Are these browser or server issues?

StackOverflowNewbie
  • 39,403
  • 111
  • 277
  • 441

11 Answers11

57

You are using Firefox, aren't you?

You could try looking at system/libraries/Upload.php line 199:

$this->_file_mime_type($_FILES[$field]);

Change that line to:

$this->_file_mime_type($_FILES[$field]); var_dump($this->file_type); die();

Then do upload your .wmv file. It would show something like application/octet-stream or whatever. Add that to your mimes.php. Hope this help =)

Similar answer here

Some links:

Community
  • 1
  • 1
Rocco
  • 1,087
  • 12
  • 21
  • OK, your suggestion worked. I updated to the latest stable version of CI and noticed that the mimes.php doesn't have `wmv` anymore. So I added back the original array + application/octet-stream. Thanks! – StackOverflowNewbie Jun 07 '12 at 13:01
  • Agree with Eric. Had to add text/plain for uploading a css file. – Aaron Feb 11 '13 at 05:35
  • On codeigniter3 go to line no. 454 and add this line var_dump($this->file_type); die(); to get file type. If file type is not in your mimes.php then add it. – Avnish Tiwary Oct 07 '16 at 06:16
  • I'm having identical installations on localhost and online. I even tried your method and the mime type is correctly shown in the file. But I cannot upload online. The upload only works on localhost. BTW, I can upload images. But not the Excel files. Any thoughts? Thanks! – itsols Jun 07 '17 at 05:31
5

$config["allowed_types"] ="*";

Kanchi
  • 75
  • 1
  • 2
4
  1. Upload your .wmv file to the server using FTP/SFTP/scp/etc where you run the CodeIgniter application
  2. On the server where you have uploaded the .wmv file, execute the following PHP code

    <?php
    $file_path = 'CHANGE_THIS_TO_ABSOLUTE_FILE_PATH_THAT_YOU_HAVE_UPLOADED.wmv';
    $finfo = finfo_open(FILEINFO_MIME);
    $mime = finfo_file($finfo, $file_path);
    var_dump($mime);
    ?>
    
  3. Save the code as mime.php

  4. Run in the terminal - $ php mime.php
  5. If it dumps any other value than you already have for wmv, append that value into wmv mime types.

Also check PHP_VERSION in other development machines. finfo_open is introduced in PHP 5.3. The development machine where upload is working may have an older version of PHP or it may have right mime type for the wmv.

2

Answer for to Upload .doc file in CodeIgniter

Change this

'doc'   =>  'application/msword',

With this on line no 95 (application/config/mimes.php file)

'doc'   =>  array('application/vnd.ms-word','application/msword'),
Ankur
  • 5,086
  • 19
  • 37
  • 62
Anil
  • 21
  • 1
2

did you try using mime types instead of extensions in $config['allowed_types']?

write it like this

$config["allowed_types"] = "video/x-msvideo|image/jpeg|video/mpeg|video/x-ms-wmv";
Taha Paksu
  • 15,371
  • 2
  • 44
  • 78
1

Check your mimes.php file in application/config. It should return an array of mime types e.g return $mimes; at end of file or return array(..... in start of mimes array.

1

For Codeigniter 3.1.7 if you are providing a custom file name via $config['file_name'] ensure the filename has the file extension as well.

William
  • 740
  • 2
  • 11
  • 18
1

I've recently had some very similar problems with the Codeigniter's Upload Class.

The *allowed_types* doesn't seem to be working. For example, I wanted to allow .PNG images to be uploaded, but it wouldn't allow them through it's filter. I ended up investigating it further. I allowed all file types to be temporarily uploaded, I uploaded a .PNG, and then dumped the upload data ($this->upload->data());). For some reason, it thought the MIME type was text/plain! This might be related to your problem.

There are some solutions to this I found surfing some forums where you can modify/extend the class or core, but they didn't work for me -- sorry. I believe it's a Codeigniter Core bug (I think the issue has already been opened with EllisLabs). I ended up hard-coding the damn thing anyways! Well, I hope this helps you some.

Basic example/work-around,

//replace with your allowed MIME types
if ($_FILES['name_of_form_upload']['type'] != 'image/jpeg' && $_FILES['name_of_form_upload']['type'] != 'image/png' && $_FILES['name_of_form_upload']['type'] != 'image/gif') {
    $data['message'] = '<div class="message">That file type is not allowed!</div>';
    $this->load->view('home_view', $data);
} else {
    //run upload code
}

Edit: Formatting/Grammar

envysea
  • 1,033
  • 1
  • 9
  • 16
0

This is actually a bug in the core.. I upgraded to latest version of CI and the bug disappeared.

thedjaney
  • 1,126
  • 2
  • 10
  • 28
0

if you need the simple way to solve this,there is an easy solution for it. open "system/libraries/Upload.php" file

line no 465
you will see 
if ( ! $this->is_allowed_filetype())
        {
            $this->set_error('upload_invalid_filetype', 'debug');
            return FALSE;
        }


just make it true

    if ( ! $this->is_allowed_filetype())
            {
                $this->set_error('upload_invalid_filetype', 'debug');
                return TRUE;
            }

note : it will stop your all file type validation.
0

Change IN application\config\Mimes.php

Anand
  • 1