2012-11-08 36 views
6

嗨我有一些上傳文件的一部分麻煩。基本上,我有一個帶有上傳徽標部分的表單,其中如果用戶沒有選擇徽標,他/她仍然可以同時更新他/她的個人資料,用戶也可以選擇上傳徽標,但它有一些限制。現在我不知道如何驗證該部分以瞭解他/她是否選擇了圖像。以下是我在CI控制器中的代碼:驗證是否選擇了一個文件上傳codeigniter

if (//CHECK IF THERE IS A FILE FOR UPLOAD){ 
       $new_name = $data['id']; 

       $config['upload_path'] = './Logos/'; 
       $config['allowed_types'] = 'jpg|png|gif'; 
       $config['max_size'] = '100'; 
       $config['max_width'] = '100'; 
       $config['max_height'] = '100'; 

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

        if (! $this->upload->do_upload()) 
        { 
         array_push($data['error_message'], "You have the following errors in your entry:\n"); 
         array_push($data['error_message'], "- Logo must be 100x100 pixels in size not exceeding 100KB with JPG,PNG or GIF format"); 
         array_push($data['error_message'], "\nLogo upload failed."); 
         $data['check_error'] = true; 
        } 
        else 
        { 
         $data = array('upload_data' => $this->upload->data()); 

         //DO UPDATE PART HERE 
         $file = $data['upload_data']['file_name']; 
         rename($config['upload_path'] . $file, $config['upload_path'] .$new_name.'.jpg'); 
         //GO TO SETTINGS 
         $this->load->helper('url'); 
         redirect($data['base'].'settings'); 
        } 
       } 

if中的部分是我想要設置驗證的地方。我嘗試了一些技巧,但所有這些都無法正常工作。

在此處,如果某個ID的標誌已經存在,它只是顯示他/她的標誌,否則會顯示默認的標誌我認爲部分:

<tr> 
    <td><p class="titles">Logo</p></td> 
    <td> 
    <div> 
     <input type="text" id="fileName" class="file_input_textbox" readonly="readonly"> 

     <div class="file_input_div"> 
      <input type="button" value="Browse" class="file_input_button" /> 
      <input type="file" class="file_input_hidden" onchange="javascript: document.getElementById('fileName').value = this.value" id="upload" name="userfile" /> 
     </div> 
    </div> 
    </td> 
</tr> 

<tr> 
<td> 
</td> 
<td> 
<p class="titles">100px x 100px jpg, png or gif only.</p> 
</td> 
</tr> 

<tr> 
<td><p class="titles">Current Logo</p></td> 
<td> 
    <img src="<?php if(is_array(@getimagesize($base."Logos/".$id.".jpg"))){echo $base."Logos/".$id.".jpg";}else{echo $base."Logos/default.jpg";} ?>" style="margin:0px 0px 0px 90px;"/> 
</td> 
</tr> 

回答

13

我通常會得到本地PHP來檢查,如果該文件是在獲取CI的上傳類之前上傳以執行作業:

if (isset($_FILES['upload_field_name']) && is_uploaded_file($_FILES['upload_field_name']['tmp_name'])) { 
    //load upload class with the config, etc... 
    $this->load->library('upload', $config); 
} 
+0

我已經嘗試了isset($ _ FILES ['upload_field_name']),但不是更高版本。將嘗試這個並儘快回覆你。謝謝! :) – KaHeL