簡體   English   中英

無法使用Codeigniter上傳圖片

[英]unable to upload image using codeigniter

請幫我。 這是我在php Codeigniter中的第一個項目。 其實我是Java開發人員。

我想將兩個圖像路徑上載到我的表中,並將其圖像上載到我的根文件夾中,例如,上載除了圖像之外,我在同一表中還有很多字段。 我可以從表單中添加/編輯這些字段。(我成功地使用codeigniter實現了)

但目前我在圖片上傳中遇到問題。 我不知道如何使用Codeigniter上傳圖片。 自兩天以來,我一直試圖自己做,但是我解決不了問題

錯誤:我沒有看到任何錯誤。 只需將0值作為圖像路徑插入到我的數據庫表中即可。 我認為我嘗試上傳圖片的方式不正確。

myviews.php

 <? echo form_open_multipart('Booksetups/book'); ?>


                 <input type="file" name="img1" /> 
                 <input type="file" name="img2" />
               <?php          
                                               <br/>
                          <? echo  form_submit($submitbtn);   echo form_reset($resetbtn); ?>  
                 <? echo form_close(); ?>  

要記住的第一件事是CI不會將$_FILES添加到輸入對象。 您將需要訪問$_FILES['img1']因此這些:

'img1'=>$this->input->post('img1'),//image path is not inserting But all other fields are inserting into db 
'img2'=>$this->input->post('img2'),//image path is not inserting

應該是這樣的:

'img1'=>$_FILES['img1']['name'],//image path is not inserting But all other fields are inserting into db 
'img2'=>$_FILES['img2']['name'],//image path is not inserting

取決於您希望存儲在數據庫中的內容。 您可以通過上載類重命名文件等。 我建議閱讀這些文檔。

其次,您似乎沒有在調用實際的上傳方法:

$this->upload->do_upload()

不確定是否需要此配置,但是...如果您想要多個配置,那么如果您希望它們具有不同的路徑,則必須為多個文件重新定義配置...

$config['upload_path'] = 'uploads/'; 
$config['allowed_types'] = 'gif|jpg|jpeg|png'; 
$config['max_size'] = '1000'; 
$config['max_width'] = '1920'; 
$config['max_height'] = '1280';  
$this->load->library('upload', $config);
$this->upload->do_upload("img1");

$config['upload_path'] = 'some_other_dir/'; 
$config['allowed_types'] = 'gif|jpg|jpeg|png'; 
$config['max_size'] = '1000'; 
$config['max_width'] = '1920'; 
$config['max_height'] = '1280';  
$this->upload->initialize($config);
$this->upload->do_upload("img2");

如果您不希望它們具有不同的路徑,則可以像在示例中一樣加載庫,並在不傳遞任何參數的情況下調用do_upload()

如果我錯過了重點,或者您需要更多信息,請告訴我,我可能會更新。

問題是您在服務器端使用了html。 PHP不懂HTML,但不懂PHP。 客戶端或瀏覽器知道HTML。

<input type="file" name="img1" /> 
<input type="file" name="img2" />

使用適當的php方法生成html。

第二件事,您的文件上傳參數不同於Codeigniter用戶指南

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

錯誤的文件路徑可能會導致其他問題

看來您沒有初始化上傳類庫:

$this->upload->initialize($config);

您是否還要加載庫:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM