Valor_
Valor_

Reputation: 3591

Uploading files with codeigniter (3.1) fails


I'm having a problem with uploading files with Codeigniter framework. Currently is working like this:
If i don't select any file in HTML form, then variables like $title = $this->input->post('title'); gets value,
but when i select file in html form, then all variables are null and i get message file is not selected.

My .htaccess file

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

My autoload.php file

$autoload['helper'] = array('url', 'file', 'form');
$autoload['libraries'] = array('database', 'session', 'email', 'form_validation');
$autoload['model'] = array('model_services' => 'ms', 'Model_categories' => 'mc');

My controller which is rendering view:

 public function add_catalog() {
        if ($this->is_admin() == TRUE) {
            $data['cat'] = $this->mc->get();
            $this->load->view('admin_view_map/admin_header');
            $this->load->view('admin_view_map/admin_add_catalog', $data);
            $this->load->view('admin_view_map/admin_footer');
        } else {
            $this->index();
        }
    }

HTML:

<?php echo form_open_multipart('admin321/insert_catalog'); ?>
    *title: <input type="text" name="title" />
    sub_title: <input type="text" name="sub_title" />
    Facebook url: <input type="text" name="facebookUrl" />
    Twitter url: <input type="text" name="twitterUrl" />
    Google +: <input type="text" name="googlePlus" />
    description: <input type="text" name="description" />
    pdf: </span> <input type="file" name="pdf" />
    categories:
    <select name="categories" >
        <?php foreach ($cat as $r) {
                echo '<option value=' . $r->id_category . ' >' . $r->category . '</option>';
        } ?>
    </select>
    <input type="submit" value="Add" class="submit">
<?php echo form_close(); ?>

PHP:

public function insert_catalog(){
    $title = $this->input->post('title');
    $sub_title = $this->input->post('sub_title');
    $facebookUrl = $this->input->post('facebookUrl');
    $twitterUrl = $this->input->post('twitterUrl');
    $googlePlus = $this->input->post('googlePlus');
    $description = $this->input->post('description');
    $category = $this->input->post('categories');
    var_dump($title,$sub_title,$facebookUrl,$twitterUrl,$googlePlus,$description,$category);

    $config['upload_path'] = './assets/pdf/';
    $config['allowed_types'] = 'pdf';
    $this->load->library('upload', $config);

    if (!$this->upload->do_upload('pdf')){
        var_dump($this->upload->display_errors());
        die();
    }else{
        $pdf = array('upload_data' => $this->upload->data());
        var_dump($pdf);
        die();
    }

}

This make result like this:

null
null
null
null
null
null
null
string '<p>You did not select a file to upload.</p>' (length=43)

I'm using WAMP server and this are my php.ini configuration

; Whether to allow HTTP file uploads.
; http://php.net/file-uploads
file_uploads = On

; Temporary directory for HTTP uploaded files (will use system default if not
; specified).
; http://php.net/upload-tmp-dir
upload_tmp_dir = "e:/wamp/tmp"

; Maximum allowed size for uploaded files.
; http://php.net/upload-max-filesize
upload_max_filesize = 64M

Upvotes: 0

Views: 523

Answers (3)

Kisaragi
Kisaragi

Reputation: 2218

Check your php's installation settings in php.ini regarding upload configuration

upload_max_filesize  = 10M
post_max_size  = 10M

Upvotes: 1

Arvind Jaiswal
Arvind Jaiswal

Reputation: 442

Load form helper on your controller

function __construct(){

    parent::__construct();
    $this->load->helper('form');

}

Upvotes: 1

Sundaze
Sundaze

Reputation: 305

I don't really get it.

<?php foreach ($cat as $r) {
        echo '<option value=' . $r->id_category . ' >' . $r->category . '</option>';
} ?>

What are you echoing here ? Where do you get $cat from ?

Upvotes: 1

Related Questions