我如何在Codeigniter上传图像

时间:2013-08-19 22:49:54

标签: php codeigniter

我在codeigniter中上传图片时遇到问题,所以我有控制器上传这样的图像: -

 public function index()
{
    $this->load->model('blog');
    $type = "text";
    if (isset($_POST['post'])) {
        if (isset($_POST['type']) && $_POST['type'] == "image") {
            $type = "image";
        }
        if (strlen($_FILES['inputUpProfile']['name']) > 0) {
            $config['upload_path'] = './uploads/';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '2048';
            $config['encrypt_name'] = true;
            $this->load->library('upload', $config);
            if (!$this->upload->do_upload('inputUpProfile')) {
                $error = $this->upload->display_errors();
                if (is_array($error)) {
                    foreach ($error as $er) {
                        $this->errors[] = $er;
                    }
                } else {
                    $this->errors[] = $error;
                }
            } else {
                $updata = $this->upload->data();
                $imagePath = './uploads/' . $eventpic;
                if (file_exists($imagePath)) {
                    @unlink($imagePath);
                }
                $eventpic = $updata['raw_name'] . $updata['file_ext'];
            }
        }
        $result = $this->blog->addPost($_SESSION['user_id'], $type, $this->input->post('post'), $eventpic);
    }

    $result = $this->blog->getPosts($_SESSION['user_id'], 0, 10);
    $this->template->build("home_view", array("response" => $result));
}

和视图是这样的: -

<div class="textstatus">
    <input id="inputUpProfile" name="inputUpProfile"
           class="inputUpProfile hidefile" type="file"/>
    <input type="button" id="PicUpProfile" class="sentpic" value="addpic">
    <input name="post" type="text" id="text" placeholder="message ...">
    <input type="submit" id="sent" value="Send">
</div>
</form>
</div>

当我在我的网站上传图片时,错误显示如下: -

Severity: Notice

Message: Undefined index: inputUpProfile

Filename: controllers/home.php

Line Number: 47

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: eventpic

Filename: controllers/home.php

Line Number: 74

A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at D:\AppServ\www\sys\system\core\Exceptions.php:185)

Filename: core/Common.php

Line Number: 442

所以代码中的问题在哪里。

/ * ** * ** * ** * ** * ***

编辑: -

Array ( [upload_data] => Array ( [file_name] => 67429_133961013479569_306349156_n3.jpg [file_type] => image/jpeg [file_path] => D:/AppServ/www/d5n/rashaqa2/uploads/ [full_path] => D:/AppServ/www/d5n/rashaqa2/uploads/67429_133961013479569_306349156_n3.jpg [raw_name] => 67429_133961013479569_306349156_n3 [orig_name] => 67429_133961013479569_306349156_n.jpg [client_name] => 67429_133961013479569_306349156_n.jpg [file_ext] => .jpg [file_size] => 34.05 [is_image] => 1 [image_width] => 720 [image_height] => 540 [image_type] => jpeg [image_size_str] => width="720" height="540" ) ) 

我需要从这个数组[file_name]保存在DB中,我该怎么读这个。

1 个答案:

答案 0 :(得分:1)

  if($_FILES['nameofinputtype']['error'] == UPLOAD_ERR_OK)
    {
       // A file was uploaded

            $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);
            $imagen = $this->upload->do_upload();
    }