上传时重命名文件

时间:2016-11-06 09:52:41

标签: php jquery html ajax file-upload

我正在使用文件上传器的拖放版本,可在以下位置找到:https://github.com/CreativeDream/php-uploader

但是它使用真实姓名上传文件而不是重命名。我想在上传文件时重命名该文件。

这是ajax_upload_file.php:

<?php
    include('class.uploader.php');

    $uploader = new Uploader();
    $data = $uploader->upload($_FILES['files'], array(
        'limit' => 10, //Maximum Limit of files. {null, Number}
        'maxSize' => 10, //Maximum Size of files {null, Number(in MB's)}
        'extensions' => null, //Whitelist for file extension. {null, Array(ex: array('jpg', 'png'))}
        'required' => false, //Minimum one file is required for upload {Boolean}
        'uploadDir' => '../uploads/', //Upload directory {String}
        'title' => array('name'), //New file name {null, String, Array} *please read documentation in README.md
        'removeFiles' => true, //Enable file exclusion {Boolean(extra for jQuery.filer), String($_POST field name containing json data with file names)}
        'replace' => false, //Replace the file if it already exists  {Boolean}
        'perms' => null, //Uploaded file permisions {null, Number}
        'onCheck' => null, //A callback function name to be called by checking a file for errors (must return an array) | ($file) | Callback
        'onError' => null, //A callback function name to be called if an error occured (must return an array) | ($errors, $file) | Callback
        'onSuccess' => null, //A callback function name to be called if all files were successfully uploaded | ($files, $metas) | Callback
        'onUpload' => null, //A callback function name to be called if all files were successfully uploaded (must return an array) | ($file) | Callback
        'onComplete' => null, //A callback function name to be called when upload is complete | ($file) | Callback
        'onRemove' => null //A callback function name to be called by removing files (must return an array) | ($removed_files) | Callback
    ));

    if($data['isComplete']){
        $files = $data['data'];

        echo json_encode($files['metas'][0]['name']);
    }

    if($data['hasErrors']){
        $errors = $data['errors'];
        echo json_encode($errors);
    }

    exit;
?>

上传类文件非常大,所以我不会在这里粘贴它。您可以在30秒内从上面给出的链接下载源代码。请帮我。感谢。

1 个答案:

答案 0 :(得分:1)

相应于此插件的文档:

  • 标题新文件名

    • null 以原始文件名上传
    • 字符串自定义文件名|使用:auto,name,{{random}},{{file_name}},{{file_size}},{{timestamp}},{{date}},{{extension}},{{.extension}}
    • <强>阵列

      • 字符串自定义文件名|使用:auto,name,{{random}},{{file_name}},{{file_size}},{{timestamp}},{{date}},{{extension}},{{.extension}}

      • 数字随机名称长度

因此,您需要选择所需的名称类型。我建议使用随机名称,用法是:

interspace :: Eq(a) => a -> a -> a->[a] -> [a]
interspace m n q[] = []
interspace m n q (x:xs)| m==x && q==(head xs)  = n: x : insert m n (headxs)++interspace m n q (xs)
    | otherwise = x : interspace m n q xs

这样你就可以创建一个带有相应文件扩展名的32个字符随机名称。