Upload ZIP-files with CodeIgniter?


Question

I am using the following code to upload and extract a ZIP-archive in PHP using CodeIgniter.

    $config['upload_path'] = 'backups/';
    $config['allowed_types'] = 'zip';
    $config['file_name'] = $project_id;

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

    if ( file_exists('backups/' . $project_id . '.zip') ) {
        try {
            delete_file('backups/' . $project_id . '.zip');
        } catch (exception $e) {
            // do nothing...
        }
    }

    if ( $this->upload->do_upload('zip_archive') ) {
        $upload_data = $this->upload->data();

        $file_name = 'backups/'.$upload_data['file_name'];

        $this->sync_model->extract_zip_archive($project_id, $file_name, TRUE);

        echo "Success";
    } else {
        echo $this->upload->display_errors('<p>','</p>');
    }

I get the error though "The filetype you are attempting to upload is not allowed." even though it is a valid .zip file I am uploading. (It's size does not exceed the maximum allowed)

This problem also occurs with every filetype I've tried that's not an image.

Any pointers? Cheers!

1
1
9/13/2009 10:19:28 AM

Accepted Answer

I solved it using the code provided by "kofic" here: http://ellislab.com/forums/viewthread/113029/

2
6/27/2014 10:55:02 AM

$config['allowed_type']

Should be a list of mime types, but it just so happens that sometimes just the file extension will work (this is the case with images at least).

Try:

$config['allowed_type'] = 'application/zip';

Although these are also mime types for zip:

application/x-zip
application/x-zip-compressed
application/octet-stream
application/x-compress
application/x-compressed
multipart/x-zip

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon