2013-08-19 64 views
0

我有問題,當CI中上傳圖片,所以我有控制器,上傳圖片是這樣的: -如何上傳圖像笨

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
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(); 
    } 
+0

你能看到我的編輯貼嗎我需要讀我的數組 –

+0

[upload_data] [file_name]這會給你文件名 – Prasannjit