我正在使用DropzoneJS脚本上传图片并使用drag& drop,但现在我正在寻找一个解决方案,以便在上传到服务器文件夹之前如何添加带有文件名的当前时间戳,因为如果该文件已存在于文件夹中,我无法上传相同的图像。
我还在下面提到了stackoverflow链接,但我很困惑在哪里实现它。
答案 0 :(得分:22)
请检查我使用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<fileList.length;f++){
if(fileList[f].fileName == file.name)
{
rmvFile = fileList[f].serverFileName;
}
}
if (rmvFile){
$.ajax({
url: "http://localhost/dropzone/sample/delete_temp_files.php",
type: "POST",
data: { "fileList" : rmvFile }
});
}
});
},
url: "http://localhost/dropzone/sample/upload.php"
});
});
<强> upload.php的强>
<?php
$ds = DIRECTORY_SEPARATOR; // Store directory separator (DIRECTORY_SEPARATOR) to a simple variable. This is just a personal preference as we hate to type long variable name.
$storeFolder = 'uploads'; // Declare a variable for destination folder.
if (!empty($_FILES)) {
$tempFile = $_FILES['file']['tmp_name']; // If file is sent to the page, store the file object to a temporary variable.
$targetPath = dirname( __FILE__ ) . $ds. $storeFolder . $ds; // Create the absolute path of the destination folder.
// Adding timestamp with image's name so that files with same name can be uploaded easily.
$date = new DateTime();
$newFileName = $date->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 强>
<?php
$ds = DIRECTORY_SEPARATOR; // Store directory separator (DIRECTORY_SEPARATOR) to a simple variable. This is just a personal preference as we hate to type long variable name.
$storeFolder = 'uploads';
$fileList = $_POST['fileList'];
$targetPath = dirname( __FILE__ ) . $ds. $storeFolder . $ds;
if(isset($fileList)){
unlink($targetPath.$fileList);
}
?>
希望这对使用ajax上传图像和使用ajax删除有帮助:)
我从以下参考文献中找到:
Dropzone.js- How to delete files from server? Dropzone.js remove button with 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;
}
}
}
答案 1 :(得分:21)
要在每次上传前为文件名添加时间戳,您有两个选项,具体取决于您的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;
}
});
});
快乐的编码, Kalasch
答案 2 :(得分:0)
我刚使用标准的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
这对我来说效果很好,实施起来非常简单。可能不建议将.jpg扩展名用于硬编码,但在我的场景中我只能获得jpg文件类型。