Factory Girl
Factory Girl

Reputation: 897

codeigniter file upload failure

Can't upload file. $this->upload->do_upload('anonsphoto'); returns false. Can't find out why.

I added upload library to autoload this way: $autoload['libraries'] = array('upload');

Here is my view:

<?php
echo form_open_multipart('InterviewController/saveInterview');

$anons_data = array(
    'name'        => 'anons',
    'id'          => 'anons_area',
    'value'       => 'Введите анонс'
);
echo form_textarea($anons_data); ?>

<p>Картинка анонса</p>
<?php
echo form_upload('anonsphoto'); ?>

Here is controller which uploads file:

public function saveInterview() {
    $config['upload_path'] = '/application/upload/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '10000';
    $config['file_name'] = 'img_' . (string) time();
    $this->upload->initialize($config);

    $this->upload->do_upload('anonsphoto');
    return;
}

Uploading file is 3 kb sized png file. I can't understand why do_upload is returning false. Any ideas?

Upvotes: 3

Views: 9487

Answers (2)

Marin Sagovac
Marin Sagovac

Reputation: 3972

File uploading class says all what you need. https://www.codeigniter.com/userguide3/libraries/file_uploading.html

This is a key:

$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';

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

// Alternately you can set preferences by calling the initialize function. Useful if you auto-load the class:
$this->upload->initialize($config);

"." (dot) means currently directory of upload path. So your app is not working because you are on root path not started with (dot).

Your folder should be with full permission (CHMOD) to 0755.

This:

$config['upload_path'] = './uploads/'; 

means: currently directory and to uploads directory

or:

$config['upload_path'] = $_SERVER['DOCUMENT_ROOT'].'/application/upload/';

means: your full document URI path and on directory '/application/upload/' as Can says.

Don't forget to put (dot) . between $_SERVER['DOCUMENT_ROOT'] and '/application/upload'.

Max size should be in KiB values: $config['max_size'] = '10000';

Hope this helps for understanding how it works.

Upvotes: 3

Can Geliş
Can Geliş

Reputation: 1474

I think your upload path is wrong. try using

$config['upload_path'] = $_SERVER['DOCUMENT_ROOT'].'/application/upload/';

and make sure that the directory is writable.

Upvotes: 0

Related Questions