Welcome Guest, Not a member yet? Register   Sign In
Simple upload problem?
#1

[eluser]Wonder Woman[/eluser]
Hi,

I have a problem with my code and I would be really grateful if someone could help me resolve it please.

Basically, when I try and upload an image it says that I have not selected a file, when I know I have. Whilst I want this fixed I was also wondering if you know how I can upload this one file to two different folders?

Controller:

Code:
$config['upload_path'] = $_SERVER['DOCUMENT_ROOT'].'/website/uploads/';
$config['allowed_types'] = 'gif|jpg|png|bmp|jpeg|pdf|doc';
$config['max_size'] = 200;
$this->load->library('upload');
$this->upload->initialize($config);
if(!$this->upload->do_upload())
{
   $data['upload_errors'] = array('error' => $this->upload->display_errors());
}
else {
   $data['upload_data'] = array('upload_data' => $this->upload->data());
}

View:

Code:
echo '<p>'.form_upload('file_name', 'Upload').'</p>';

If you could help me that would be great, many thanks in advance.
#2

[eluser]Cristian Gilè[/eluser]
By default the upload routine expects the file to come from a form field called userfile, and the form must be a "multipart type:

Code:
echo form_open_multipart('controller/method');
....
....
echo '<p>'.form_upload('userfile', '').'</p>';
....
....
echo form_close();

If you want to maintain 'file_name' as field name, you have to pass it to the do_upload method as follow:

Code:
if( ! $this->upload->do_upload('file_name'))
{
....
....
}


Cristian Gilè
#3

[eluser]Wonder Woman[/eluser]
Ah I see, thanks so much, got that working now.

Do you know how I can take an uploaded image and resize it and place it in another folder? I have tried but I keep getting errors Sad
#4

[eluser]Cristian Gilè[/eluser]
Code:
$config['image_library']  = 'gd2';
$config['source_image']   = '/path/to/the/uploaded/image/mypic.jpg';
$config['new_image']      = '/path/to/the/new/destination/image/mypic.jpg';
$config['maintain_ratio'] = TRUE;
$config['width']          = 75;
$config['height']         = 50;

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

if ( ! $this->image_lib->resize())
{
    echo $this->image_lib->display_errors();
}

After a successful image resize, unlink the image in the upload folder, if you don't need it.


Cristian Gilè




Theme © iAndrew 2016 - Forum software by © MyBB