2012-01-18 82 views
0

上傳圖片和codeigniter有困難,它一直說我沒有選擇要上傳的文件。用codeigniter上傳圖片

這是我的代碼

function do_upload() 
{ 
    $config['upload_path'] = './uploads/'; 
    $config['allowed_types'] = 'jpg|png'; 
    $config['max_size'] = '3000'; 
    $config['max_width'] = '1024'; 
    $config['max_height'] = '768'; 

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

    if (!$this->upload->do_upload()){ 
     echo $this->upload->display_errors(); 
    }else{ 
     echo $this->upload->data(); 
    } 
} 

它甚至不將圖像發送到該目錄。

+0

你會得到任何錯誤? – Dau 2012-01-18 14:49:28

+0

也許一些輸出會有幫助嗎? – swatkins 2012-01-18 15:14:36

+0

我沒有得到,請解釋。我不親! – 2012-01-18 15:27:34

回答

1

試試這個函數:

function upload_done() { 
$config['overwrite'] = TRUE; 
$config['encrypt_name'] = FALSE; 
$config['remove_spaces'] = TRUE; 
$config['upload_path'] = '/var/www/uploadfiles/'; // use an absolute path 
$config['allowed_types'] = 'jpg|png'; 
$config['max_size'] = '0'; 
if (! is_dir($config['upload_path'])) die("THE UPLOAD DIRECTORY DOES NOT EXIST"); 
$this->load->library('upload',$config); 
if (! $this->upload->do_upload()) 
{ 
    echo "UPLOAD ERROR ! ".$this->upload->display_errors(); 
} else { 
    echo "THE IMAGE HAS BEEN UPLOADED : "; var_dump($this->upload->data()); 
} 
} 
+0

感謝上傳完美的圖片。 – 2012-01-20 13:46:42

+0

但我想立即運行一個查詢並存儲我的圖像表上的圖像的完整路徑如何獲得完整路徑。這是包含所有圖像信息的數組。 $ this-> upload-> data() – 2012-01-20 13:48:33

+0

您會在 中找到所需的內容** $ t = $ this-> upload-> data(); $ full_path_to_image = $ t ['full_path']; ** 來存儲所有的數組,只要做: **'$ this-> db-> insert('images_table',$ t);'** – sly63 2012-01-21 15:02:25

0

確保您的HTML看起來像下面。

<input type="file" name="userfile" size="20" /> 

笨的上傳庫假設名稱爲「userfile的」

0

您需要根據您所給出的形式輸入名稱更改調用do_upload()。比方說,你有這樣的代碼在你的HTML:

<input type="file" name="image" /> 

你需要改變:

$this->upload->do_upload() 

$this->upload->do_upload('image') 

默認情況下笨假設名稱爲 「userfile的」。

+0

感謝imsge上傳,很好。 – 2012-01-20 13:50:41