ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Upload file

August 20, 2010 7:30am

Subscribe [6]
  • #1 / Aug 20, 2010 7:30am

    williams94

    4 posts

    Why upload files such as doc or docx are not able to upload? but for mp3 files, jpg, zip can.
    Is rar files can also be?

    function lakukan()
      {
    $config['upload_path'] = './uploads';
    $config['allowed_types'] = 'exe|psd|pdf|xls|ppt|php|php4|php3|js|swf|Xhtml|zip|mid|midi|mp2|mp3|wav|bmp|gif|jpg|jpeg|png|html|htm|txt|rtf|mpeg|mpg|avi|doc|docx|xlsx';

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

    if ( ! $this->upload->do_upload())
    {
    $error = array('error' => $this->upload->display_errors());
    echo $this->upload->display_errors();
    $this->load->view('upl');

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

    $this->load->view('home');
    }
      }

  • #2 / Aug 20, 2010 8:27am

    JoostV

    527 posts

    The upload library checks the extension (e.g. .doc) against the mime-type that is sent by the browser.

    Some browsers send mime-types that are not in the mimes array that Codeigniter uses to check. You may be running an older version of CI, or using an exotic browser.

    Check which mime-type is sent by the browser, and then check if this mime-type is set for this extension, in the mimes array: config/mimes.php

    You can add new mime types to the mimes array if you want to check for exotic filetypes such as .vcf.

  • #3 / Aug 20, 2010 12:52pm

    old_guy

    12 posts

    I’m having a similar problem but with pdf. I’ve tried with doc, txt, odt with no problems. But for some reason pdf file extensions are not recognized and the upload fails.

    My current setting in application/config/mimes.php are

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

    I’m using xampp

  • #4 / Aug 20, 2010 12:56pm

    JoostV

    527 posts

    Some browsers will send ‘application/x-download’ as a header for pdf
    See if this works:

    'pdf' => array('application/pdf', 'application/x-pdf', 'application/x-download'),
  • #5 / Aug 20, 2010 1:00pm

    old_guy

    12 posts

    Nope….still fails

  • #6 / Aug 20, 2010 1:06pm

    JoostV

    527 posts

    Echo the mimetype to the screen and add it to your mimes array. Say the name of the upload fields is ‘my_upload’:

    var_dump($_FILES['my_upload']['type']);
  • #7 / Aug 20, 2010 1:39pm

    old_guy

    12 posts

    That did it! mimetype is ‘application/download’. Thanks!!

  • #8 / Aug 20, 2010 6:45pm

    JoostV

    527 posts

    You’re welcome 😊
    You may have to add some more mime types for other browsers, or on browser updates. For some reason, not every browser/version sends the same mime type on upload 😖

  • #9 / Aug 21, 2010 11:49am

    williams94

    4 posts

    could you tell me, where i should to put the:

    echo var_dump($_FILES[‘userfile’][‘type’]);

    I’ve a view like this: (field=userfile)

    <?$this->load->view(‘mkiri’); ?>
     

    <td valign=‘top’ width=‘100%’ class=‘isitext’>
    <table><tr><td><?echo ‘<table><tr><td>’.$this->session->flashdata(‘salah’).’</td></tr></table>’;?></td></tr></table>

    <?php echo form_open_multipart(‘upl/lakukan’);?>

    <input type=“file” name=“userfile” id=“userfile” /><br >

     


    <input type=“submit” value=“upload” />

    </form>
    <?echo var_dump($_FILES[‘userfile’][‘type’]);
    ?>
    <table>
    <tr>
    <td>
    </table>

    </td>
    <td> </td>

    <? $this->load->view(‘bawah’);?>

  • #10 / Aug 21, 2010 12:10pm

    JoostV

    527 posts

    You can show the error anywhere you like

    CONTROLLER

    if(!$this->upload->do_upload('userfile')) {
        $data['upload_error'] = $this->upload->display_errors();
        $data['upload_error'] .= 'You tried to upload a file with mime-type ' . $_FILES[‘userfile’][‘type’] . '';
    }
    else {
        $data['upload_error'] = '';
    }

    VIEW
    Pass $data to view and echo upload_error somewhere.

    echo $upload_error;
  • #11 / Aug 21, 2010 12:17pm

    williams94

    4 posts

    doc = “application/download”
    and i’ve already change the mimes into:

    ‘doc’=>‘application/download’,

    but it doesn’t work.

  • #12 / Aug 21, 2010 12:23pm

    JoostV

    527 posts

    Did you try

    'doc' => '"application/download"'

    EDIT: so that’s including the double quotes 😊

  • #13 / Aug 21, 2010 12:45pm

    williams94

    4 posts

    in firefox doc=application/download
    in ie doc=application/msword
    so I’ve change the mimes:

    'doc'    =>    array('application/download','application/msword'),

    but it doesn’t work. (doc,docx,xls,xlsx)

  • #14 / Aug 21, 2010 2:27pm

    JoostV

    527 posts

    can you post the entire code?

  • #15 / Aug 27, 2010 3:50am

    williams94

    4 posts

    view:

    <?     $this->load->view('mkiri'); ?>
        
    
    <td valign='top' width='100%' class='isitext'>
    <table><tr><td><?echo '<table><tr><td><b><i>'.$this->session->flashdata('salah').'</i></b></td></tr></table>';?></td></tr></table>
    
    <?php echo form_open_multipart('upl/lakukan');?>
    
    <input type="file" name="userfile" id="userfile" /><br >
    
    
    
    
    
    
    <input type="submit" value="upload" />
    
    </form>
    <?echo var_dump($_FILES['userfile']['type']);
    
    
    echo $upload_error;
    ?> 
    
    <table>
    <tr>
    <td>
    </table>
    
    </td>
    <td> </td>
    
    <? $this->load->view('bawah');?>

    controller :

    <?php
    
    class upl extends Controller {
        
        function upl()
        {
            parent::Controller();
        }
        
        function index()
        {    
            $this->load->view('upload');
        }
    
        function lakukan()
        {
            $config['upload_path'] = './uploads';
            $config['allowed_types'] = 'exe|psd|pdf|xls|ppt|php|rar|php4|php3|js|swf|Xhtml|zip|mid|midi|mp2|mp3|wav|bmp|gif|jpg|jpeg|png|html|htm|txt|rtf|mpeg|mpg|avi|doc|docx|xlsx';
            $this->load->library('upload', $config);
        
            if ( ! $this->upload->do_upload())
            {
                $this->session->set_flashdata('salah','Upload anda gagal, silahkan coba kembali');
                echo $this->upload->display_errors();
                $this->load->view('upload');
            }    
            else
            {
                $data = array('upload_data' => $this->upload->data());
                $this->session->set_flashdata('salah','Upload anda telah berhasil');
                echo $this->upload->display_errors();
                $this->load->view('upload');
            }
    }
    }
    ?>

    mimes:

    $mimes = array(    'hqx'    =>    'application/mac-binhex40',
                    'cpt'    =>    'application/mac-compactpro',
                    'csv'    =>    array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel'),
                    'bin'    =>    'application/macbinary',
                    'dms'    =>    'application/octet-stream',
                    'lha'    =>    'application/octet-stream',
                    'lzh'    =>    'application/octet-stream',
                    'exe'    =>    'application/octet-stream',
                    'class'    =>    'application/octet-stream',
                    'psd'    =>    'application/x-photoshop',
                    'so'    =>    'application/octet-stream',
                    'sea'    =>    'application/octet-stream',
                    'dll'    =>    'application/octet-stream',
                    'oda'    =>    'application/oda',
                    'pdf'    =>    array('application/pdf', 'application/x-download'),
                    'ai'    =>    'application/postscript',
                    'eps'    =>    'application/postscript',
                    'ps'    =>    'application/postscript',
                    'smi'    =>    'application/smil',
                    'smil'    =>    'application/smil',
                    'mif'    =>    'application/vnd.mif',
                    'xls'    =>    array('application/excel', 'application/vnd.ms-excel'),
                    'ppt'    =>    array('application/powerpoint', 'application/vnd.ms-powerpoint'),
                    'doc'    =>    array('application/download','application/msword'),
                    'docx'    =>    'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
                    'xlsx'    =>    'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
                    'word'    =>    array('application/msword', 'application/octet-stream'),
    
    
                    'swf'    =>    'application/x-shockwave-flash',
                    'rar'    =>    'application/x-rar-compressed',
                    'sit'    =>    'application/x-stuffit',
                    'xht'    =>    'application/xhtml+xml',
                    'zip'    =>  array('application/x-zip', 'application/zip', 'application/x-zip-compressed'),
    
                    'aif'    =>    'audio/x-aiff',
                    'aiff'    =>    'audio/x-aiff',
                    'aifc'    =>    'audio/x-aiff',
                    'ram'    =>    'audio/x-pn-realaudio',
                    'gif'    =>    'image/gif',
                    'jpeg'    =>    array('image/jpeg', 'image/pjpeg'),
                    'jpg'    =>    array('image/jpeg', 'image/pjpeg'),
                    'jpe'    =>    array('image/jpeg', 'image/pjpeg'),
                    'png'    =>    array('image/png',  'image/x-png'),
                    'tiff'    =>    'image/tiff',
                    'tif'    =>    'image/tiff',
                    'css'    =>    'text/css',
                    'html'    =>    'text/html',
                    'htm'    =>    'text/html',
                    'shtml'    =>    'text/html',
                    'txt'    =>    'text/plain',
                    'text'    =>    'text/plain',
                    'log'    =>    array('text/plain', 'text/x-log'),
    
                );
.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases