首页 > 解决方案 > Dropzone.js 文件未将文件上传到服务器

问题描述

编辑:我还想补充一点,无论是 js 还是 PHP,我都没有收到任何错误。

从技术上讲,我正在使用一个表格,我希望卖家填写产品的必要内容,还包括要在购物区显示的产品图片。我讨厌html5的默认输入类型文件,我很难隐藏它,所以我遇到了dropzone。现在 dropzone 在客户端有点工作,但似乎在服务器端没有工作。没有文件上传到我希望发送的重定向上。

这对我来说是一种新事物,所以我不知道该怎么做。

这是html代码

<form enctype='multipart/form-data' action='upload.php' method='POST'>
<div class='form-group'>
                    <input class='form-control' type='text' placeholder='Title' id='titleInput' name='titleInput'>
                </div>
<div class='form-group'>
                    <input class='form-control' type='number' placeholder='Price' id='priceInput' name='priceInput'>
                </div>
<div class='dropzone' id='myDropzone'>
                </div>
<button id='submit-all' type='submit' name='Post'>Submit<\button>
</form>

javascript代码

Dropzone.options.myDropzone= {
    url: 'upload.php',
    paramName: "file",
    autoProcessQueue: false,
    uploadMultiple: false,
    //parallelUploads: 5,
    //maxFiles: 5,
    maxFilesize: 1,
    acceptedFiles: 'image/*',
    addRemoveLinks: true,
    init: function() {
        dzClosure = this; // Makes sure that 'this' is understood inside the functions below.

        // for Dropzone to process the queue (instead of default form behavior):
        document.getElementById("submit-all").addEventListener("click", function(e) {
            // Make sure that the form isn't actually being sent.
            //e.preventDefault();
            e.stopPropagation();
            dzClosure.processQueue();
        });

        //send all the form data along with the files:
        this.on("sendingmultiple", function(data, xhr, formData) {
            formData.append("titleInput", jQuery("#titleInput").val());
            formData.append("priceInput", jQuery("#priceInput").val());
        });

    }
}

和upload.php代码

<?php

if(isset($_POST['Post'])){
    if(isset($_FILES['file']['name'])){
        $imageName = $_FILES['file']['name'];
        $imageNameTemp = $_FILES['file']['tmp_name'];
        echo $imageName;
    }
    else{
        $imageName = "";
        echo "null";
    }
  $errorMessage = "";

  if($imageName != "") {
    $targetDir = "assets/images/productImages/";
    $imageName = $targetDir . uniqid() . basename($imageName);
    $imageFileType = pathinfo($imageName, PATHINFO_EXTENSION);

    if($_FILES['file']['size'] > 5000000) {
      $errorMessage = "Sorry your file is too big!";
      $uploadOk = 0;
    }

    if(strtolower($imageFileType) != "jpeg" && strtolower($imageFileType) != "png" && strtolower($imageFileType) != "jpg") {
      $errorMessage = "Sorry, only jpeg, jpg and png files are allowed";
      $uploadOk = 0;
    }

    if(strtolower($imageFileType) == "jpeg" || "jpg"){
      $src = imagecreatefromjpeg($imageNameTemp);
    }

    if(strtolower($imageFileType) == "png"){
      $src = imagecreatefrompng($imageNameTemp);
    }

    if($uploadOk == 1) {

      list($width_min, $height_min) = getimagesize($imageNameTemp);
      if($width_min > 1920){
        $newwidth_min = 1920;
        $newheight_min = ($height_min / $width_min) * $newwidth_min;
      }
      else if($height_min > 1920){
        $newheight_min = 1920;
        $newwidth_min = ($height_min / $width_min) * $newheight_min;
      }
      else{
        $newheight_min = $height_min;
        $newwidth_min = $width_min;
      }
      $tmp_min = imagecreatetruecolor($newwidth_min, $newheight_min);
      imagecopyresampled($tmp_min, $src, 0, 0, 0, 0, $newwidth_min, $newheight_min, $width_min, $height_min);
      imagejpeg($tmp_min, $imageName);
      $uploadOk = 1;

    }

      else {
        //image did not upload
        $uploadOk = 0;
      }

    }
}

?>

因此,当它重定向到upload.php 时,它会回显null,因为如果没有发送文件,那是我写的。

请帮忙

标签: javascriptphpdropzone

解决方案


推荐阅读