1

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!

3
  • $config['max_size'] = '100'; means the max file size is 100 bytes, is that what you want? also what error are you getting? Commented May 7, 2010 at 13:07
  • alas i'm getting no error and it's not the file size. Commented May 7, 2010 at 13:13
  • i was being an idiot. height and width was too big! Commented May 7, 2010 at 13:16

2 Answers 2

2

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!

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

Comments

1

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.

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.