2012-12-11 52 views
0

我建立功能上傳zip文件,但是當用戶上傳文件兩次它沒有刪除第一個上傳的文件,但加增量號文件名((file.zip,文件1日前上傳刪除.ZIP,file2.zip ,,,等)如何檢查,如果文件與笨

,所以我想告訴函數,當用戶上傳的文件名相同刪除第一個文件並上傳第二個文件或更換...任何人幫助我如何做到這一點...

/** 
* change book source file 
* 
* @param integer $book_id 
*/ 
public function upload_book_zip($book_id) { 
    $vars = array(); 
    $vars['upload_path'] = PUBPATH . 'global/modules/bookstore/files/books_source_file/'; 
    $vars['allowed_types'] = 'zip'; 
    $vars['max_size'] = '30720'; 
    $vars['book_id'] = $book_id; 

    $book = $this->d_book->find_by_id($book_id); 
    if (isset($_POST['submit'])) { 
     $file_name = $this->upload($vars); 
     if ($file_name === NULL) { // error happens while uploading file 
      $vars['upload_errors'] = $this->upload->display_errors("<p class='notification n-error'>", "</p>"); 
     } else { 
      $this->d_book->update_one_field($book_id, 'bo_path_zip', $file_name); 
      $this->session->set_flashdata('success_msg', lang('file_uploaded')); 
      redirect('bookstore/admin_d_book/'); 
     } 
    } else { 
     $vars['upload_errors'] = NULL; 
    } 

    if ($book->bo_path_zip) { // load cover image 
     $vars['file_path'] = base_url() . 'global/modules/bookstore/files/books_source_file/' . $book->bo_path_zip; 
    } else { 
     $vars['file_path'] = NULL; 
    } 
    $vars['controller_name'] = 'admin_d_book'; 
    $this->view('bookstore/admin/change_zip_file', $vars); 
} 

    /** 
* 
* @param array $config the configuration array 
* @return string 
* 
*/ 
private function upload($config) { 
    $this->load->library('upload', $config); 

    if (!$this->upload->do_upload("file")) { 
     return $uploadData['file_name'] = NULL; 
    } else { 
     $uploadData = $this->upload->data(); 
     log_message('debug', 'file has been uploaded ok - file name is ' . $uploadData['file_name']); 
     return $uploadData['file_name']; 
    } 
} 
+1

你可以看看這個頁面http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html – RockOnGom

回答

1

你可以做,通過添加

$vars['overwrite'] = TRUE; 

更好地檢查文件上傳library

1

設置overwrite爲true的配置數組中傳遞

$vars = array(); 
    $vars['upload_path'] = 'filepath here'; 
    $vars['allowed_types'] = 'zip'; 
    $vars['max_size'] = '30720'; 
    $vars['book_id'] = $book_id; 

    // add this line 
    $vars['overwrite'] = true; 

    // the old file will now get overwritten 
    $file_name = $this->upload($vars) 
0

更好的方法是檢查文件的MD5或SHA1校驗。

如果兩個用戶上傳相同名稱的服務器兩個不同的文件,在服務器上的文件將被覆蓋,這是不可能的只是名稱distingush文件。

此外,它可以很容易地在PHP中完成。