jQuery File Upload by bluimp, how to replace instead of renaming

筅森魡賤 提交于 2019-12-05 06:17:28
Primoz Rome

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.

Vladimir Salguero

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

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!