views:

1320

answers:

2

Hi friends,

I know there are many tutorials online, but I could not make them work :( maybe something particularly wrong with my system :/

My Controller localpath is: /localhost/rl/applications/backend/controller/

Controller:

function do_upload()
{
 $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);

 if ( ! $this->upload->do_upload())
 {
  $error = array('error' => $this->upload->display_errors());

  $this->load->view('add_image', $error);
 } 
 else
 {
  $data = array('upload_data' => $this->upload->data());

  $data['id'] = $this->input->post['id_work'];
  $this->load->view('add_image', $data);
 }

}

My View localpath is: /localhost/rl/applications/backend/view/

View:

echo form_open_multipart('do_upload'); 
    <ul class="frm">
     <li><label>File: *</label><input type="file" name="userfile" class="frmlmnt" size="50" /></li>  
     <li><label></label><input type="submit" class="btn" value="Upload" /></li>
    </ul>
 </form>

Maybe I do something wrong with path

A: 

I have no idea what codeigniter is, but I see three things right off:

  1. Your function doesn't have anything passed to it. I'm not sure if the "this" variable handles that, but maybe you should pass it the upload URL?

  2. You have config['size'] set to '100'. I can only guess that size refers to the filesize, but does this extension you are using default to KB or MB?

  3. You have this bit:

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

and this bit:

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

 $data['id'] = $this->input->post['id_work'];
 $this->load->view('add_image', $data);

Which part is actually doing the uploading? If it's the first one, how does it know what to upload? I don't see that in any of the config array, and imagine "upload" is not the address of the file...

If it's the second part, you have it set to

$this->load->view('add_image', $data);

But why would you want to view it? Aren't you uploading it? I'd assume that view IS what uploads it, but you use the same method for errors in the previous part.

Anthony
Some responses in case others find this question: This code is straight from http://codeigniter.com/user_guide/libraries/file_uploading.html 1. It's a function inside a CodeIgniter (CI) controller, so everything is passed by the framework. 3. $this->upload->do_upload() inside the if statement does the actual uploading. upload_data() just returns the details of that upload. So when he sends $data to the view, he's not viewing the actual upload, he's only viewing the details.
jimyi
A: 

hey Anthony, thanks for giving time to write such detailed answer! it helped me to think over some parts :)

and later I found a great tutorial which makes exactly what i need :) and everything is working great now. thanks for help ;)

tut link which helped me: http://net.tutsplus.com/videos/screencasts/easy-development-with-codeigniter/#comment-83378