2013-10-30 83 views
1

我嘗試使用文件上傳器在頁面上編寫代碼。生成的頁面必須具有用戶上傳的圖像路徑名稱作爲背景的css代碼。我'使用codeigniter file_write。我有投入來獲得bg顏色和標題。我有一個觀點頁面layout.php中(777權限) 但是,我的代碼無法正常工作,點擊上傳按鈕後,appers的錯誤:「上傳路徑似乎並沒有有效」代碼點火器文件上傳和文件寫入

class Upload extends CI_Controller { 

    public function __construct() 
     { 
      parent::__construct(); 
      $this->load->helper(array('form', 'url', 'file', 'directory')); 
     } 

    public function index() 
     { 
      $this->load->view('upload_form', array('error' => '')); 
     } 

    public function do_upload() 
     { 

      $path = $this->input->post('new-folder'); 
      $title = $this->input->post("title"); 
      $bgcolor = $this->input->post("bgcolor"); 

      $upload_path_url = site_url().'uploads/'.$path.'/'; 

      $config['upload_path'] = './uploads/'.$path.'/'; 
      $config['allowed_types'] = 'gif|jpg|jpeg|png'; 
      $config['max_size'] = '2000'; 
      $config['max_width'] = '1920'; 

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

      if (! $this->upload->do_upload()) { 
       $error = array('error' => $this->upload->display_errors('<p class="error">','<p>')); 
       $this->load->view('upload_form', $error); 

      } else { 
       $data = $this->upload->data(); 

       //set the data for the json array 
       $info->name = $data['file_name']; 
       $info->size = $data['file_size']; 
       $info->type = $data['file_type']; 
       $info->url = $upload_path_url .$data['file_name']; 

       $info->thumbnail_url = $upload_path_url .$data['file_name']; 
       $info->delete_url = base_url().'upload/deleteImage/'.$data['file_name']; 
       $info->delete_type = 'DELETE'; 

       //this is why we put this in the constants to pass only json data 
       if (IS_AJAX) { 
        echo json_encode(array($info)); 

       // so that this will still work if javascript is not enabled 
       } else { 
        echo $upload_path_url; 

        if(!is_dir($path)) 
        { 
         mkdir($upload_path_url,0777,TRUE); 

        } 

        $page_layout = $this->load->view('layout'); 
        $write_data = '<div style="width:100%; height:100%; background: url(../uploads/'.$data['file_name'].')"></div></body></html>' ; 

        if (! write_file($page_layout . $write_data, 'a+')) 
        { 
         $msg_file = '<label>Unable to generate the page</label>'; 
        } 
        else 
        { 
         $msg_file = '<label>Layout Page was generated!</label>'; 
        } 


        $file_data['msg_file'] = $msg_file; 
        $file_data['title'] = $title; 
        $file_data['bgcolor'] = $bgcolor; 
        $file_data['upload_data'] = $this->upload->data(); 
        $this->load->view('upload_success', $file_data); 
       } 
      } 
     } 
} 

form_upload.php的部分:

<!-- Site title --> 
<label>Title:</label> 
<input type="text" size="40" name="title" /> 
<br /><br /> 
<label>Background color:</label> 
<input type='text' value='#fff' name='bgcolor' id='bg' size="10" /> 
<br /><br /> 

上載success.php的一部分:

if (isset($msg_file)) { echo $msg_file; } 
    echo '<br /><br />'; 
    echo '<label>Site title: '. $title.'</label>'; 
    echo '<br />'; 
    //echo $write_data; 
    echo '<br />'; 
    echo '<label>background-color: '. $bgcolor.'</label>'; 
    echo '<br />'; 

回答

1

消息「上傳路徑似乎並沒有有效」你引用10行。 這意味着該目錄不存在和/或具有不良的權限。查看你的代碼,$ path是通過表單中的POST數據得到的,並且你不會在你上傳之前的任何地方創建這個文件夾。我不認爲CI的Upload類會創建一個上傳文件夾,如果這個文件夾不存在的話。我沒有測試過,但嘗試像下面這樣:

$config['upload_path'] = './uploads/'.$path.'/'; 
[...] 

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

if (!empty($path) && !file_exists($config['upload_path'])) { 
    if (!mkdir($config['upload_path'], 0777, true)) { 
    die('Failed to create folders...'); 
    } 
} 

if (! $this->upload->do_upload()) { 
[...] 

我不是在談論POST數據檢查,因爲這是另一回事。

+0

謝謝!!! Obrigado !! –

相關問題