[英]Invalid upload directory with CodeIgniter 1.7.2
我正在嘗試使用CodeIgniter 1.7.2創建圖像上傳表單
但是,當我嘗試上傳圖像時,CI會通知我上傳目錄無效。
為了檢查這一點,我將一個文件上傳到名為test.txt
的uploads
目錄中,其內容為Hello, World!
然后我回顯了其中的內容,瞧,它問好了。 除此之外,我還可以瀏覽目錄並在瀏覽器中查看目錄的內容,因此目錄肯定存在並且可以查看。
控制器:
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);
}
知道為什么我的上傳目錄無效嗎?
$config['upload_path'] = base_url().'uploads/';
您應該將其更改為:
$config['upload_path'] = './uploads/';
當無法找到目錄時,甚至目錄不可讀時,CI都會生成該錯誤。 首先嘗試找出目錄是否正常,例如:
echo base_url().'uploads/';
如果出現,請檢查目錄權限。 chmod為755。
如果即使目錄許可也可以,請嘗試在您的上傳路徑中添加以下行。
$config['upload_path'] = $_SERVER['DOCUMENT_ROOT'] . '/' . base_url().'uploads/';
通過回顯路徑,確保路徑正常。
希望能有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.