我正在使用DropzoneJS脚本通过拖放上传图像,但现在我正在寻找一个解决方案,以便在上传到服务器文件夹之前如何添加带有文件名的当前时间戳,因为我无法上传相同的图像,如果文件已存在于该文件夹中。
我也在下面提到了stackoverflow链接,但我很困惑在哪里实现它。
这里是dropzone.js script供参考
请检查我使用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;
}
}
}
要在每次上传之前使用时间戳为文件名添加前缀,您有两个选项,具体取决于您的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
这对我很有用,并且实现起来非常简单。可能不建议将.jpg扩展名用于硬编码,但在我的场景中我只能获得jpg文件类型。