gpt4 book ai didi

jquery - Dropzone.js - 如何在上传到文件夹之前更改文件名

转载 作者:行者123 更新时间:2023-12-03 21:53:28 34 4
gpt4 key购买 nike

我正在使用DropzoneJS用于通过拖放上传图像的脚本,但现在我正在寻找一种解决方案,如何在上传到服务器文件夹之前添加当前时间戳和文件名,因为如果文件已存在于中,我将无法上传相同的图像文件夹。

我还引用了下面的 stackoverflow 链接,但我很困惑在哪里实现它。

  1. https://stackoverflow.com/a/23805488/3113858
  2. https://stackoverflow.com/a/19432731/3113858

这里是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"
});

});

上传.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

还在 dropzone.js 文件中第 #1110 行后添加以下代码,以防止用户上传同名的重复文件:)

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

关于jquery - Dropzone.js - 如何在上传到文件夹之前更改文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24859005/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com