2012-10-15 20 views
4

首先所有的jQuery插件可以在這裏找到:通過bluimp jQuery的文件上傳,如何更換,而不是重命名

https://github.com/blueimp/jQuery-File-Upload

我使用的腳本的PHP版本,我試圖替換具有相同名稱的圖像,而不是重命名它們。

我想我已經找到了重命名的功能,但我嘗試的編輯不起作用。

protected function upcount_name_callback($matches) { 
     $index = isset($matches[1]) ? intval($matches[1]) + 1 : 1; 
     $ext = isset($matches[2]) ? $matches[2] : ''; 
     return ' ('.$index.')'.$ext; 
    } 

    protected function upcount_name($name) { 
     return preg_replace_callback(
      '/(?:(?: \(([\d]+)\))?(\.[^.]+))?$/', 
      array($this, 'upcount_name_callback'), 
      $name, 
      1 
     ); 
    } 

有什麼建議嗎?我在這個地方找遍了,但無濟於事。

連接到PHP的類,它的上傳: https://github.com/blueimp/jQuery-File-Upload/blob/master/server/php/upload.class.php

有什麼建議?

編輯:

我一直在努力,只是返回null這些功能,但只是掛劇本,可能是因爲它不知道該怎麼辦,如果文件名是一樣的。

+0

更換get_file_name()函數,你找到了一個解決方案?我也需要它。謝謝 –

+0

什麼都沒有。我正在考慮在github上建議這個功能,但是據我所知,bluimp與服務器端腳本無關。但你可以upvote的問題:) –

+0

是的同意。與此同時,我發現了我自己的解決方案。我會發佈一個答案,我用他們提供的PHP服務器端腳本做了什麼,以便覆蓋文件... –

回答

7

您可以改變他們的PHP服務器端腳本,以覆蓋文件而不是創建新的唯一文件名。只是改變上傳處理類功能get_file_name

原來的功能:

protected function get_file_name($file_path, $name, $size, $type, $error, 
     $index, $content_range) { 
    $name = $this->trim_file_name($file_path, $name, $size, $type, $error, 
     $index, $content_range); 
    return $this->get_unique_filename(
     $file_path, 
     $this->fix_file_extension($file_path, $name, $size, $type, $error, 
      $index, $content_range), 
     $size, 
     $type, 
     $error, 
     $index, 
     $content_range 
    ); 
} 

它改成這樣:

protected function get_file_name($file_path, $name, $size, $type, $error, 
      $index, $content_range) { 
     $name = $this->trim_file_name($file_path, $name, $size, $type, $error, 
      $index, $content_range); 
     return $name; 
    } 

這樣,現有的文件將被覆蓋,如果上傳的文件名是一樣的服務器上的現有文件。

0

在使用此文件自定義函數的index.php需要UploadHandler.php

protected function trim_file_name($file_path, $name, $size, $type, $error, 
      $index, $content_range) { 

    // Remove path information and dots around the filename, to prevent uploading 
    // into different directories or replacing hidden system files. 
    // Also remove control characters and spaces (\x00..\x20) around the filename: 
    $name = trim(basename(stripslashes(unique_custom_file_name)), ".\x00..\x20"); 
    // Use a timestamp for empty filenames: 
    if (!$name) { 
     $name = str_replace('.', '-', microtime(true)); 
    } 
    return $name; 
} 
0

你也可以使用覆蓋作爲一個選項這樣的:

$options = array(
    'overwrite' => $overwrite 
); 
$upload_handler = new UploadHandler($options); 
在UploadHandler.php

: 添加默認參數

function __construct($options = null,...){ 
      ... 
      'overwrite' => false, 
      ... 
    } 

然後通過

protected function get_file_name($file_path, $name, $size, $type, $error, 
      $index, $content_range) { 
     $name = $this->trim_file_name($file_path, $name, $size, $type, $error, 
      $index, $content_range); 
     if($this->options['overwrite']) 
      return $name; 
     else 
      return $this->get_unique_filename(
       $file_path, 
       $this->fix_file_extension($file_path, $name, $size, $type, $error, 
        $index, $content_range), 
       $size, 
       $type, 
       $error, 
       $index, 
       $content_range 
      ); 
    } 

問候

相關問題