Codeigniter image upload not working

时间秒杀一切 提交于 2020-01-11 12:33:13

问题


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!


回答1:


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!




回答2:


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.



来源:https://stackoverflow.com/questions/2788700/codeigniter-image-upload-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!