2011-04-24 98 views
1

我有兩個上傳腳本,只用不同的目錄就可以完成類似的功能。上傳目標文件夾似乎不可寫。 (Codeigniter)

function town_upload() { 


     //Do not create a thumbnail 
     $create_thumb = false; 

     //Create the path for the upload 
     $path = './public/uploads/towns/' . $this->uri->segment(3); 

     $config['upload_path'] = $path; 
     $config['allowed_types'] = 'jpg'; 
     $config['max_size'] = '2048'; 
     $config['max_width'] = '1024'; 
     $config['max_height'] = '768'; 
     $config['remove_spaces'] = TRUE; 
     $config['overwrite'] = $this->image_upload_overwrite; 

     if ($this->input->post('image_type') == 'main_image') { 
      $config['file_name'] = 'main.jpg'; 
      $create_thumb = true; 
     } 

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

     if (!$this->upload->do_upload()) { 

      $this->session->set_flashdata('upload_error', array('error' => $this->upload->display_errors())); 

     } else { 

      //sucess so now we create the thumbnail 
      $this->session->set_flashdata('upload_success', $this->upload->data()); 

示例:如果我有一個ID爲6的城鎮,上傳目錄將爲public/uploads/towns/6。這個工作完全正常,因爲該目錄是在城鎮創建時創建的。除了路徑爲public/uploads/hotels/[ID]外,我對酒店的功能幾乎完全相同。我已經將權限設置爲777,只是爲了從現在的等式中刪除該權限。

我無法弄清楚這一點,因爲它們都是非常相似的功能。這裏是酒店上傳代碼:

function hotel_upload() { 

     //Create the path for the upload 
     $path = './public/uploads/hotels/' . $this->uri->segment(3); 

     chmod($path, 0777); 

     $config['upload_path'] = $path; 
     $config['allowed_types'] = 'jpg'; 
     $config['max_size'] = '2048'; //kilobytes (2048 = 2mb) 
     $config['max_width'] = '1024'; 
     $config['max_height'] = '768'; 
     $config['remove_spaces'] = TRUE; 
     $config['overwrite'] = TRUE; 

     if ($this->input->post('image_type') == 'main_image') { 
      $config['file_name'] = 'main.jpg'; 
     } 

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

     if (!$this->upload->do_upload()) { 

      $this->session->set_flashdata('upload_error', array('error' => $this->upload->display_errors() . ' <br/>Path: ' . $path . '<br/>Image Type: ' . $this->input->post('image_type'))); 

     } else { 

      //sucess so now we create the thumbnail 
      $this->session->set_flashdata('upload_success', $this->upload->data()); 

酒店功能給我的錯誤「上傳目的地文件夾似乎不可寫」。

這裏是網頁的HTML表單:

TOWN:

<?php echo form_open_multipart('admin/town_upload/' . $this->uri->segment(3));?> 

      <input type="file" name="userfile" size="20" /> 

      <br /><br /> 

      <p> 
       <input type="radio" name="image_type" checked="checked" class="radio" value="main_image" /> <label>Main Image</label> 
       <input type="radio" name="image_type" class="radio" value="other_image" /> <label>Other Image</label> 
      </p> 

      <input type="submit" class="submit short" value="upload" /> 

     <?php echo form_close(); ?> 

HOTEL:

<?php echo form_open_multipart('admin/hotel_upload/' . $this->uri->segment(3));?> 

      <input type="file" name="userfile" size="20" /> 

      <br /><br /> 

      <p> 
       <input type="radio" name="image_type" checked="checked" class="radio" value="main_image" /> <label>Main Image (shown when visitors search the website)</label> 
       <input type="radio" name="image_type" class="radio" value="other_image" /> <label>Standard Image</label> 
      </p> 

      <input type="submit" class="submit short" value="upload" /> 

     <?php echo form_close(); ?> 

提前感謝! (

+1

是否有必要回答該問題的所有示例代碼?我很懷疑。提供一個重現問題的簡短例子。 – cweiske 2011-04-24 19:01:53

+0

目標目錄是否真的存在?你在哪裏創建它?兩個腳本都在同一個目錄中嗎? – 2011-04-24 19:04:33

+0

@cweiske這就是酒店上傳腳本所需的所有代碼,因爲do_upload方法總是返回false。通過HTML表單,我提交了一個滿足所有要求的簡單圖像。 – SamV 2011-04-24 19:05:39

回答

0

好的,剛剛回來。我認爲權限被搞亂了,我銷燬並重新創建文件夾,它似乎工作。

2

你的代碼並沒有檢查chmod的工作,例如,默認情況下,apache喜歡以自己的用戶或nobody運行,而不是你作爲用戶運行。在您的文件可能會發現用戶的當前運行的犯規擁有該文件,因此文件模式失敗。

我會檢查第一件事是PHP認爲它運行的用戶,文件的所有者。

還有is_writable函數,你可以檢查目錄是否可寫。

+0

我甚至不需要chmod它,因爲目錄已經是777,我已經閱讀過這個內容,但我不明白爲什麼town_upload函數可以工作,並且酒店功能不像他們基本上是一樣的東西。 只是檢查它是否可寫,它返回true。真的不知道發生了什麼事! – SamV 2011-04-24 19:14:31

+0

你可以做'echo $ config [「upload_path」];'在你設置它後,我們可以看到它是你期望的結果 – Dormouse 2011-04-24 19:16:02

+0

它返回「./public/uploads/hotels/7」,Filezilla說它在那裏以及。 – SamV 2011-04-24 19:29:01

2

我剛碰到同樣的問題,找到了可能的原因是行爲。就我而言,我已經創建了上傳目錄。我升級了PHP,其中放入了一個新的php.ini文件,該文件有safe_mod = On.現在,當我在刪除目錄後重新創建目錄時,新的php參數將引發此錯誤消息。

難道你只是有可能在PHP升級之前創建鎮迪爾樹,然後你的酒店目錄樹?