Download jQuery file from bluimp, how to replace instead of renaming

First of all, the jQuery plugin can be found here:

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

I am using the PHP version of the script and I am trying to replace images with the same name instead of renaming them.

I think I found functions that rename, but the fixes I tried do not work.

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 ); } 

Any suggestions? I searched everywhere, but to no avail.

Link to the php class that is loading: https://github.com/blueimp/jQuery-File-Upload/blob/master/server/php/upload.class.php

Any suggestions?

Edit:

I tried to just return null for these functions, but it just hangs the script, perhaps because it does not know what to do if the file name matches.

+4
source share
3 answers

You can change your side of the PHP script server so that the files are overwritten instead of creating a new unique file name. Just change the get_file_name load handler class function:

Original Function:

  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 ); } 

change this to the following:

 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; } 

Thus, the existing file will be overwritten if the downloaded file name is the same as the existing file on your server.

+8
source

Use this custom function in the index.php file, requiring 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
source

you can also use rewrite as an option as follows:

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

in UploadHandler.php: add default parameter

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

then replace the get_file_name () function with

  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 ); } 

considers

0
source

All Articles