2014-02-24 73 views
1

我想上傳一個文件,並以標題名稱替換它的名字,但我無法通過,即使沒有在控制器迴響在探查一個POST獲取文件名。上傳文件,並顯示其名稱作爲後

我還需要重命名,但在此之前,我需要知道的崗位價值,即文件名。

在這裏,我張貼我的代碼。

我的觀點

<?php echo form_open_multipart('emailtemplate/do_upload');?> 

<tr class='odd gradeX'> 
    <td class='center'> 
     <input type="file" name="userfile" size="20" /> 
    </td> 
    <td class='center'> 
     <input placeholder='Title For Your File' name='title' class='form-control'> 
    <td class='center'> 
     <input placeholder='Comment On File' name='comment' class='form-control'> 
    </td> 
</tr> 

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

</form> 

我的控制器

function do_upload() 
{ 
    $this->load->helper('form'); 
    $this->load->helper('html'); 
    $config['upload_path'] = './uploads/'; 
    $config['allowed_types'] = 'gif|jpg|png'; 
    $config['max_size'] = '1000'; 
    $config['encrypt_name']  = true; 
    //$file=$this->input->post('userfile'); 
    $this->load->library('upload', $config); 
    $this->upload->data(); 
    $file_name = $this->upload->do_upload('userfile'); 
    echo $file_name; 


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

     $this->load->view('add/addfile', $error); 
    } 
    else 
    { 
     $data = array('upload_data' => $this->upload->data()); 

     $this->load->view('add/addfile', $data); 
    } 
    $this->output->enable_profiler(true); 
} 
+0

CI中訪問後值通過'$這 - >輸入 - >交的();'相同$ _POST –

+0

如何重命名上傳的文件? – amoeba

回答

0

要重命名與崗位價值上傳文件:

//get extension 
    $ext = end(explode(".", $_FILES["userfile"]['name'])); 
    $config['file_name'] = $this->input->post("title").'.'.$ext; 

刪除$config['encrypt_name'] = true;。否則你的文件名將被加密。

確保您title沒有不是在你上傳文件夾中。或者添加一些隨機數/標題的字符。

0

我創建了一個功能上傳文件: -

需要參數即FIELD_NAME

public function fileUpload($field) { 
    //get file extension 
    $exts = @split("[/\\.]", $_Files[$field][name]); 
    $n = count($exts) - 1; 
    $ext = $exts[$n]; 
    //create image name or replace it with your desired name 
    $imageName = date("Ymdhis") . time() . rand(); 
    $config['file_name'] = $imageName; //set the desired file name 
    $config['overwrite'] = true;   
    $config['allowed_types'] = str_replace(',', "|", $ext); 
    $config['upload_path'] = ABSOLUTEPATH . 'product/original/'; 
    $config['optional'] = true; 
    $config['max_size'] = '1000'; 
    $config['max_width'] = '00'; 
    $this->load->library('upload', $config); 
    $this->upload->initialize($config); 
    if (!$this->upload->do_upload($field, true)) { 
     $error = array('error' => $this->upload->display_errors()); 
     $r = $error; 
    } else { 
     $data = array('upload_data' => $this->upload->data()); 
     $r = "sucess"; 
    }   
    return $r; 
} 
0

我已經寫代碼波紋管它可以幫助你

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

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

     if(isset($files['file']['name'])) 
     { 
      $filename = $files['file']['name']; 
      $ext = end(explode(".", $filename)); 
      $_FILES['file']['name']= date('d').date('m').date('y').'_'.date(time()).'.'.$ext; //here i am changing file name with current date as your wish to change your logic 

     // print_r($_FILES);exit; check the changed name 

      if ($this->upload->do_upload('file')) 
      { 
       $upload = array('upload_data' => $this->upload->data()); 
       $imagename = $upload['upload_data']['file_name']; //uploaded your image name 
      } 
      else 
      { 
        $error = array('error' => $this->upload->display_errors()); 
        print_r($error); //error 
      } 
     } 
0

注意:嘿user3345331,看行

$this->upload->do_upload() 

替換爲這個

$this->upload->do_upload('userfile'); 

這userfile的是輸入框,在你的HTML代碼的文件類型的ID。