2009-12-11 61 views
1

我試圖創建一個圖像上傳表單使用CodeIgniter 1.7.2使用CodeIgniter 1.7.2

然而,當我試圖上傳圖片上傳無效目錄,CI告訴我的上傳目錄是無效的。

要檢查這,我上傳一個文件名爲test.txt與內容Hello, World!uploads目錄,然後我呼應了它的內容,你瞧,它說你好。除此之外,我可以瀏覽目錄並在瀏覽器中查看它的內容,所以目錄肯定存在並且可以查看。

控制器:

class Pictures extends Site_Controller { 
    // snip... 
    public function upload() { 
     $this->load->helper('form'); 
     $this->_render('Do Upload','pictures/upload',array('msg'=>'')); 
    } 
    public function do_upload() { 
     $this->load->helper('form'); 

     $config['upload_path'] = base_url().'uploads/'; 
     // test the directory: 
     // echo file_get_contents($config['upload_path'].'test.txt'); 
     // should echo "Hello, World!" 
     $config['allowed_types'] = 'gif|jpg|png'; 
     $config['max_size'] = '2048'; 
     $config['max_width'] = '1024'; 
     $config['max_height'] = '768'; 

     $this->load->library('upload', $config); 

     if (! $this->upload->do_upload()) 
     { 
      $data = array('msg' => $this->upload->display_errors()); 

      $this->_render('Do Upload (Errors!)','pictures/upload',$data); 
     }  
     else 
     { 
      $uploaddata = $this->upload->data(); 
      $this->load->model('pictures_Model','pictures'); 
      $this->pictures->insertUploaded($uploaddata,$this->input->post('name'),$this->input->post('caption')); 

      $data['msg'] = 'Successful Upload!'; 
      $this->_render('Do Upload (Success!)','pictures/upload',$data); 
     } 
    } 
    //... 
} 

$this->_render()僅僅是一個快捷方式功能,它包裝與所述指定的數據與指定的標題的佈局指定的圖。

pictures/upload觀點:

<h1>Do Upload</h1> 
<? if (isset($msg)) echo $msg; ?> 

<?= form_open_multipart('pictures/do_upload');?> 

<input type="file" name="userfile" /> 
<input type="text" name="name" /> 
<input type="text" name="caption" /> 

<br /><br /> 

<input type="submit" value="Upload" /> 

</form> 

最後,也可能是不必要的,該模型插入:

public function insertUploaded($data,$name,$caption) { 
    $row = array(); 
    $row['filename'] = $data['file_name']; 
    $row['mime'] = $data['file_type']; 
    $row['size'] = $data['file_size']; 
    $row['name'] = $name; 
    $row['caption'] = $caption; 
    $row['img'] = file_get_contents($data['full_path']); 

    $this->db->insert('pictures',$row); 
} 

任何想法,爲什麼我的上傳目錄是無效的?

回答

6
$config['upload_path'] = base_url().'uploads/'; 

你應該更改爲:

$config['upload_path'] = './uploads/'; 
+0

這是爲啥工作? – 2009-12-11 06:48:41

+1

上傳路徑需要是服務器上的文件路徑,而不是URL。 – bradym 2009-12-18 16:25:03

1

CI產生錯誤時,它是無法找到的目錄或即使目錄不是可讀。首先嚐試找出目錄是否正常:

echo base_url().'uploads/'; 

如果有,請檢查目錄權限。 chmod設爲755.

如果即使目錄權限沒有問題,請嘗試將下面的行添加到您的上傳路徑中。

$config['upload_path'] = $_SERVER['DOCUMENT_ROOT'] . '/' . base_url().'uploads/'; 

請確保您的路徑通過回顯出現正常。

希望有所幫助。