0

I have a controller with the method 'do_upload' which should upload an image to /img but instead I'm getting the following error:

http://localhost/img/

The upload path does not appear to be valid.

This is the method of the upload class.

public function do_upload(){

        $config['upload_path']= "http://localhost/img/";
        $config['allowed_types']= 'gif|jpg|png';
        $config['max_size']='100';
        $config['max_width']='1024';
        $config['max_height']='768';


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

        if(!$this->upload->do_upload()){
            $errors=array('errors'=>$this->upload->display_errors());
            echo $config['upload_path'];
            $this->load->view('error',$errors);
        }

        else{

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

            $this->load->view('admin/admin');
        }
    }

The file permissions are set to the following (to test this works!):

drwxrwxrwx

However, if I direct my browser to http://localhost/img/ I am able to view the contents of the directory so I don't think it's a permission issue. Is there something I'm doing wrong?

Sheldon
  • 9,639
  • 20
  • 59
  • 96

4 Answers4

4

As lanzz said, try

$config['upload_path']= APPPATH;

or

$config['upload_path']= APPPATH.'img/';

File management and browser adress is rarely identical, and never with localhost.

Edit: Please see my other answer for possible paths. In most cases you might want to use FCPATH.

Community
  • 1
  • 1
Robin Castlin
  • 10,956
  • 1
  • 28
  • 44
3

You cannot upload to a URL. $config['upload_path'] must be a path to a filesystem location on your server.

lanzz
  • 42,060
  • 10
  • 89
  • 98
1

i faced the same problem for some hours.

The problem is about the file path given in config. I changed it to absolute path manually. I was working in WAMP. My project directory is 'rmp' When i used config file path as,

  $_SERVER['DOCUMENT_ROOT'].'rmp/uploads/';

Everything works fine. Dont know what's the exact problem. But this fixed my solution. Cheers!!! :)

SOLUTION 2: I did a little work and found that CI is targeting the project folder by default. So if you have img as upload folder name just make sure it is in root of codeigniter parallel to application, system,..

Like, ROOT/yourcodeigniterproject/img

In that case just give 'img/' as your upload path. Works fine.

Ankur
  • 5,086
  • 19
  • 37
  • 62
0

Old question but no one pointed that img folder need to be listed in htaccess file. Depending on how it looks like (.htaccess code), if you use CI user_guide URL page offered one, after robots.txt add pipe devider and img like:

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt|img)
RewriteRule ^(.*)$ /index.php/$1 [L]
Tpojka
  • 6,996
  • 2
  • 29
  • 39