6

First of all the jQuery plugin can be found here:

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

I'm using the PHP-version of the script and I'm trying to replace images that has the same name instead of renaming them.

I think I've found the functions that do the renaming, but the edits 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've searched for this all over the place but to no avail.

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

Any suggestions?

Edit:

I've tried to just return null on these functions, but that just hangs the script, probably because it doesn't know what to do if the filename is the same.

Albin N
  • 1,401
  • 2
  • 16
  • 27
  • Nope nothing yet. I'm thinking of suggesting this feature on github, but then again, bluimp has nothing to do with the server side script as far as I know. But you could upvote the question :) – Albin N Jun 07 '13 at 06:22
  • Yes agree. In the meanwhile I have found the solution my self. I will post an answer what I did with their provided PHP server side script so that file is overwritten... – Primoz Rome Jun 11 '13 at 07:55
  • Hi I have provided a solution to this question. Please accept my answer if it suits you. It works for me. – Primoz Rome Jun 26 '13 at 14:43

3 Answers3

9

You can alter their PHP server side script to have files overwritten instead of creating a new unique filename. Just alter upload handler class function get_file_name:

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

alter it to this:

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

This way existing file will get overwritten if the uploaded file-name is same as the existing file on your server.

Fizzix
  • 23,679
  • 38
  • 110
  • 176
Primoz Rome
  • 10,379
  • 17
  • 76
  • 108
0

Use this custom function in file index.php require 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;
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Vladimir Salguero
  • 5,609
  • 3
  • 42
  • 47
0

you can also use overwrite as an option like this:

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

in UploadHandler.php : add the default parameter

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

then replace get_file_name() function by

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

regards

Cyril Jacquart
  • 2,632
  • 3
  • 25
  • 24