Dropzone.js – 如何在上传到文件夹之前更改文件名
我正在使用DropzoneJS脚本通过拖放来上传图像,但现在我正在寻找一种解决方案,以便在上传到服务器文件夹之前如何添加带有文件名的当前时间戳,因为我无法上传相同的图像,如果文件已存在于该文件夹中。
我也在下面提到了stackoverflow链接,但我很困惑在哪里实现它。
- https://stackoverflow.com/a/23805488/3113858
- https://stackoverflow.com/a/19432731/3113858
这是dropzone.js脚本供参考
请检查我使用PHP实现的以下代码。
在索引文件中使用以下代码
$(document).ready(function() { Dropzone.autoDiscover = false; var fileList = new Array; var i =0; $("#some-dropzone").dropzone({ addRemoveLinks: true, init: function() { // Hack: Add the dropzone class to the element $(this.element).addClass("dropzone"); this.on("success", function(file, serverFileName) { fileList[i] = {"serverFileName" : serverFileName, "fileName" : file.name,"fileId" : i }; //console.log(fileList); i++; }); this.on("removedfile", function(file) { var rmvFile = ""; for(f=0;f
upload.php的
getTimestamp().$_FILES['file']['name']; $targetFile = $targetPath.$newFileName; // Create the absolute path of the uploaded file destination. move_uploaded_file($tempFile,$targetFile); // Move uploaded file to destination. echo $newFileName; } ?>
delete_temp_files.php
希望这对使用ajax上传图像和使用ajax删除有帮助:)
我从以下参考文献中找到:
Dropzone.js-如何从服务器删除文件? Dropzone.js用php删除按钮
还要在第1110行之后的dropzone.js文件中添加以下代码,以防止用户上传具有相同名称的重复文件:)
Dropzone.prototype.addFile = function(file){
if (this.files.length) { var _i, _len; for (_i = 0, _len = this.files.length; _i < _len; _i++) { if(this.files[_i].name === file.name && this.files[_i].size === file.size){ return false; } } }
参考链接: https : //www.bountysource.com/issues/2993843-dropzone-did-not-check-the-duplicate-file-on-addfile?utm_campaign=plugin&utm_content=tracker%2F283989&utm_medium=issues&utm_source=github
要在每次上传之前使用时间戳为文件名添加前缀,您有两个选项,具体取决于您的DropzoneJS版本。
较新版本的DropzoneJS 5.1+确实有一个renameFile
函数,使用如下:
... renameFile: function (file) { file.name = new Date().getTime() + '_' + file.name; } ...
在旧版本 v4.3 – v5.1中,这有点不同。
在这个版本中有一个renameFilename
选项,它的使用方式如下:
Dropzone.autoDiscover = false; $(document).ready(function () { $(".dropzone").dropzone({ renameFilename: function (filename) { return new Date().getTime() + '_' + filename; } }); });
快乐的编码,卡拉施
我只是使用标准的php文件重命名。
$targetFile = $targetPath . $_FILES['file']['name']; //the original upload $newfilename = "somename" . $variable . ".jpg"; //a new filename string rename($targetFile , $new); //rename at the end of the function