由bluimp上传的jQuery文件,如何替换而不是重命名

时间:2012-10-15 12:28:36

标签: php jquery upload

首先,可以在这里找到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,但这只是挂起脚本,可能是因为如果文件名是相同的,它不知道该怎么做。

3 个答案:

答案 0 :(得分:9)

您可以更改其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;
    }

这样,如果上传的文件名与服务器上的现有文件相同,现有文件将被覆盖。

答案 1 :(得分: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;
}

答案 2 :(得分:0)

您还可以使用覆盖作为选项,如下所示:

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

: 添加默认参数

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

然后用

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

问候