3

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?

2
  • 1
    codeigniter cannot override the upload limits set in php.ini. check what the upload_max_size value is. Commented Mar 15, 2013 at 20:23
  • 1
    Try to echo $this->upload->display_errors();. Commented Mar 15, 2013 at 20:23

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I solved problem just useing path 'applicastion/upload'. Without dot before and without /. What does it mean?
. dot make you to sure that is in your currently directory. On linux is mostly used ofted or for FTP. This will also can avoid confusion if you put / means go to root of filesystem. When you put ./ means go to current directory. Hope this helps.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.