Drew
Drew

Reputation: 3234

Codeigniter image upload not working

I can't get my images to upload no matter what i do. Below is the code that is in my model:

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());

        return $error;
    }   
    else
    {
        $data = array('upload_data' => $this->upload->data());

        return $data;
    }

Here is the code that is in my controller:

function do_upload()
{
    $this->Upload_model->do_upload();

}

And here is my form:

<html>
<head>
<title>Upload Form</title>
</head>
<body>



<?php echo form_open_multipart('upload/do_upload');?>
<?php if(isset($buttons)) : foreach($buttons as $row) : ?>
<h2><?php echo $row->name; ?></h2>
<input type="file" name="userfile" size="20" />
<input type="hidden" name="oldfile" value="<?php echo $row->image_url; ?>" />
<input type="hidden" name="id" value="<?php echo $row->id; ?>" />
<br /><br />
<label>Url: </label><input type="text" name="url" value="<?php echo $row->url; ?>" /><br /><br />
<input type="submit" value="submit" />

</form>

<?php endforeach; ?>
<?php endif; ?>

</body>
</html>

Can someone tell me what it is that i'm doing wrong? I'm a complete newbie to codeigniter and it is really leaving me at a dead end. As well as this I want to be able to write the path of the file to my database but i can't figure that out either so if someone has an answer or even point me in the right direction I would be extremely grateful. Thanks

EDIT -----------

The image i was uploading was too tall/wide. But if someone could point me in the direction of how to write the filepath to the database, that would be great!

Upvotes: 1

Views: 2811

Answers (2)

Pranav
Pranav

Reputation: 11

You can decrease the height and width of image which you are setting 1024 and 768.

$config['max_width']  = '800';
$config['max_height']  = '600';

and secondly, I may suggest, you can store just the imagename say 'abc.jpg', not the entire path of the image. You don't need to store entire path in database because you can get this path by doing same when you were setting the path while uploading an image.

Upvotes: 1

Edd Twilbeck
Edd Twilbeck

Reputation: 101

Did something similar with a project - basically you just need to turn the path into a string and write it to your db...

$fileup = $this->upload->data();
    $myfilepath = $fileup['file_name'];
    $databack = array(
        'your_field'=> 'http://www.yourserver.com/location/'.$myfilepath.'',
    ); 
    $DB->where('your_id_field', $id_value);
    $DB->update('your_table', $databack);  

The trickiest bit is just tying it to your database!

Upvotes: 2

Related Questions