2013-01-07 54 views
-2

無法上傳照片。 PHP給出錯誤您沒有選擇要上傳的文件。在視圖代碼如下的控制器在codeigniter中上傳照片無法按預期工作

<?php echo form_open_multipart('upload/do_upload_member_photo');?> 
            <div class="tab_content" id="tab2" style="display: none;"> 
             <input type="file" id="file1" name="file1" style=""> 
             <a href="#" onclick="openFileOption();return;"> <div class="one">Upload Photo/Video</div></a> 
             <a href="#"> <div class="two">Create Photo Album</div></a> 
             <textarea name="post" placeholder="What's on your mind?" style="margin: 0px; width: 437px; height: 39px; border:1px solid #999"></textarea> 
             <button type="submit" class="post_button" style="float:right; color:#fff">Post</button> 
             </div> 
           </form> 

<script> 
function openFileOption() 
{ 
document.getElementById("file1").click(); 
} 
</script> 

代碼

function do_upload_member_photo() 
    {  
     $this->load->helper(array('form', 'url')); 

     $config['upload_path'] = './uploads/'; 
     $config['allowed_types'] = 'gif|jpg|png'; 
     $config['max_size'] = '2000'; 
     $config['width'] = '150'; 
     $config['height'] = '150'; 
     $config['maintain_ration'] =TRUE; 
     $config['overwrite']=TRUE; 
     $config['file_name'] = $_FILES['file1']['name'][0];; 

     if(!file_exists('./uploads/' . $this->session->userdata('user_id'))) 
     { 
      mkdir('./uploads/' . $this->session->userdata('user_id'), 0777); 
     } 

     $config['upload_path'] = './uploads/'.$this->session->userdata('user_id'); 

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

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

      print_r($error); 
     } 
     else 
     { 
      $data = array('upload_data' => $this->upload->data()); 
     } 
    } 
+1

當您在此處發佈代碼示例時,請刪除任何註釋掉的代碼,這不屬於此處。保持小例子。我已經爲你編輯它。也請檢查並正確縮進您的代碼,包括HTML。現在它幾乎不可讀。另外你的問題是什麼? – hakre

回答

2

$this->upload->do_upload()此功能在默認情況下搜索名稱爲文件輸入userfile 但是您的文件輸入名稱爲file

所以你需要將其更改爲userfile或將文件輸入名稱作爲參數傳遞給上傳功能$this->upload->do_upload('file1')

ref:File Upload Library