Javascript 使用dropzone.js上载图像
我想使用Javascript 使用dropzone.js上载图像,javascript,php,Javascript,Php,我想使用dropzone.js上传图像 <div class="row" id="dropzone-example"> <form action="http://www.****.com/assets/Upload.php" class="dropzone bg-gray col-md-12 center-margin" enctype= multipart/form-data
dropzone.js
上传图像
<div class="row" id="dropzone-example">
<form action="http://www.****.com/assets/Upload.php" class="dropzone bg-gray col-md-12 center-margin" enctype= multipart/form-data
id="dropzone">
</form>
</div>
它返回我的console.log
值,但没有上载图像文件
我的php代码(Upload.php
):
您遇到了什么问题?在表单上拖动图像后,我的主机上的目录中没有图像文件,另一方面,我的图像现在将上载。您是否收到任何错误消息?Upload.php返回到JS函数的是什么?您尝试了哪些调试步骤?在我的控制台上,例如:已成功上载:1457360708funnel-hero_1x.jpg
您是否已检查您是否具有将文件上载到该文件夹的权限,以及该文件夹是否存在?我在本地运行了你的代码,效果很好
$(document).ready(function () {
Dropzone.autoDiscover = false;
$("#dropzone-example").dropzone({
url: "http://www.****.com/assets/Upload.php",
addRemoveLinks: true,
success: function (file, response) {
var imgName = response;
file.previewElement.classList.add("dz-success");
console.log("Successfully uploaded :" + imgName);
},
error: function (file, response) {
file.previewElement.classList.add("dz-error");
}
});
});
<?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 = '/test' . $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;
}
?>