使用dropzone.js上传图片

时间:2016-03-07 14:19:46

标签: 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
                      id="dropzone">


                </form>

            </div>
          $(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");
            }
        });
    });

它返回我的console.log值,但没有上传图片文件。

我的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 = '/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;
}
?>

enter image description here

0 个答案:

没有答案