當我嘗試上傳一張圖片,它不是在所有上傳和我的代碼是在這裏文件上傳被失敗
public function image_upload($path)
{
if($_FILES)
{
$config['upload_path'] = './uploads/'.$path.'/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '5000';
$config['max_width'] = '4000';
$config['max_height'] = '6500';
$config['file_name'] ='img';
$this->load->library('upload', $config);
if(! $this->upload->do_upload("file"))
{
$this->session->set_flashdata('message',$this->upload->display_errors());
return false;
}
else
{
$config['image_library'] = 'gd2';
$config['source_image'] = './uploads/'.$path.'/'.$this->upload->file_name;
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = FALSE;
$config['width'] = 200;
$config['height'] = 150;
$config['new_image'] = './uploads/'.$path.'/thumb/';
$this->load->library('image_lib',$config);
if(! $this->image_lib->resize())
{
$this->session->set_flashdata('message',$this->image_lib->display_errors());
return false;
}
else
{
return $this->upload->file_name;
}
}
}
}
我認爲頁是
<form role="form" action="<?php echo base_url();?>admin/add_staff" method="post" enctype="multipart/form-data">
<tr>
<td>Photo</td>
<td><input type="file" id="file" name="file"></td>
</tr>
<tr>
<td></td>
<td> <input class="btn btn-primary save-btn" type="submit" value="Save" > </td>
</tr>
</form>
我cntroller看起來像這樣
public function add_staff()
{
$data['active_mn'] = 'add_staff';
if($_POST)
{
$this->form_validation->set_rules('name', 'Name', 'required');
if($this->form_validation->run() == true)
{
if(! $this->image_upload($path = 'staff'))
{
$photo = '';
}
else
{
$photo = $this->upload->file_name;
}
$data = array('photo'=>$photo);
$status = $this->admin_model->db_insert($table='staff',$data);
if($status)
{
$this->session->set_flashdata('message','Staff added Successfully');
}
else
{
$this->session->set_flashdata('message','Insertion failed');
}
redirect('admin/view_staff');
}
}
$this->load->view('admin/add_staff',$data);
}
我已經經歷了幾種方式,但沒有得到解決這個問題。會是系統文件的問題嗎?
而你用來從$ _FILES ['file'] ['tmp_name'];'獲取文件的代碼在哪裏?哪個是物理phile(雙關語意思) – ArtisticPhoenix
檢查您試圖上傳的圖像大小。在PHP默認是8M,如果你嘗試上傳超過這個限制,它不會上傳。如果你想增加上傳限制改變php.ini新的限制 – Veer
不,我使用的圖像大小隻有不到8M –