使用Dropzone.js将图像上传到文件夹

时间:2017-10-15 11:18:49

标签: javascript php dropzone.js

我在上传特定文件夹中的图片时遇到问题。就像我正在使用表单的名称字段创建一个文件夹。我希望该图像上传到新创建的文件夹中,而不是父文件夹中。我附上我的代码请帮忙。

HTML:

<form action="assets/php/customer-query.php" method="post" class="add_customer" enctype="multipart/form-data">
    <input type="text" name="name" class="form-control" />
    <div id="dropZone" class="dropzone"></div>
    <button class="btn btn-success pull-right hidden-mobile" name="add_customer">Add Customer</button>
</form>

JS:

Dropzone.autoDiscover = false;
var myDropzone = new Dropzone('div.dropzone', {
    url: "http://localhost/build/assets/php/customer-query.php",                        
    autoProcessQueue: false,
});
$('form.add_customer').submit(function(){
    myDropzone.processQueue();
});

PHP:

// Create folder using name field from form 
$fname = $_POST['name'];
if (!file_exists('../uploads/'.$fname.'/')) {
    mkdir('../uploads/'.$fname.'/', 0777, true);
}

// Upload files to respected folder
$ds = DIRECTORY_SEPARATOR;
$storeFolder = '../uploads/'.$fname.'/';
if (!empty($_FILES)) {
    $tempFile = $_FILES['file']['tmp_name'];                 
    $targetPath = dirname( __FILE__ ) . $ds. $storeFolder . $ds;
    $targetFile =  $targetPath. $_FILES['file']['name'];
    move_uploaded_file($tempFile,$targetFile);
}

0 个答案:

没有答案