Json 编码在一台服务器上工作,在另一台服务器上工作,给出错误未定义

Json Encode working in one server and on another giving error undefined

我有非常基本的文件上传系统,使用 PHP 和 Dropzone。上传PHP 文件如下

<?php
error_reporting(E_ALL);
    // define absolute folder path
    $brand = $_POST['brand'];
    $reference = $_POST['reference'];

    $dest_folder = 'images/'.$brand.'/';
    $url = 'https://www.example.com/testupload/';
 
if(!empty($_FILES)) {

    if(!file_exists($dest_folder) && !is_dir($dest_folder)) mkdir($dest_folder);

    foreach($_FILES['file']['tmp_name'] as $key => $value) {
        
        $ext = strtolower(pathinfo($_FILES['file']['name'][$key],PATHINFO_EXTENSION));
        
       
        if(file_exists($dest_folder) && !is_dir($dest_folder)){
             continue;
        }else{
            mkdir($dest_folder);
        } 
        
        $imgName =  $brand."-".$reference.'-picture'.$key.'.'.$ext;
        
        $tempFile = $_FILES['file']['tmp_name'][$key];
        $targetFile =  $dest_folder.$imgName;
        move_uploaded_file($tempFile,$targetFile);
    }
    
    
         $dir = $dest_folder;
        $data = scandir($dir);
        $arr = [];
        
        
        foreach($data as $key=>$dataVal)
        {
            if($dataVal!='.' && $dataVal!='..'){
                
                 
                   $arr[] = $url.$dir.$dataVal; 
               
              
           }
        }
        
         $imgstring = implode(",",$arr);
        
    /**
     *  Response 
     *  return json response to the dropzone
     *  @var data array
     */
    $data = [
        "file" =>$brand,
        "dropzone" => $_POST["dropzone"],
        "img"=>$imgstring
    ];
    
    file_put_contents('abc.txt',$data);
    
    header('Content-type: application/json');
    echo json_encode($data);
    exit();

}

我的 DropZone js 如下所示

// disable autodiscover
Dropzone.autoDiscover = false;


 
var myDropzone = new Dropzone("#dropzone", {
    url: "upload.php",
    method: "POST",
    paramName: "file",
    autoProcessQueue : false,
    acceptedFiles: "image/*",
    maxFiles: 5,
    maxFilesize: 2, // MB
    uploadMultiple: true,
    parallelUploads: 100, // use it with uploadMultiple
    createImageThumbnails: true,
    thumbnailWidth: 120,
    thumbnailHeight: 120,
    addRemoveLinks: true,
    timeout: 180000,
    dictRemoveFileConfirmation: "Are you Sure?", // ask before removing file
    // Language Strings
    dictFileTooBig: "File is to big ({{filesize}}mb). Max allowed file size is {{maxFilesize}}mb",
    dictInvalidFileType: "Invalid File Type",
    dictCancelUpload: "Cancel",
    dictRemoveFile: "Remove",
    dictMaxFilesExceeded: "Only {{maxFiles}} files are allowed",
    dictDefaultMessage: "Drop files here to upload",
});

myDropzone.on("addedfile", function(file) {
    
    console.log(file);
});

myDropzone.on("removedfile", function(file) {
    // console.log(file);
});

// Add mmore data to send along with the file as POST data. (optional)
myDropzone.on("sending", function(file, xhr, formData) {
    formData.append("dropzone", "1"); // $_POST["dropzone"]
});

myDropzone.on("error", function(file, response) {
    console.log(response);
});




/**
 *  Add existing images to the dropzone
 *  @var images
 *
 */


 

    $("body").on("submit","#dropzone-form",function(e){
         e.preventDefault();
         
           var brand      =    $('body .brand').val();
                var reference   =  $("body .reference").val();
                
                if(brand=='' || reference==''){
                    alert('Please check your Input');
                    return false;
                }
         
         myDropzone.on("sending", function(file, xhr, formData){
                var brand      =    $('body .brand').val();
                var reference   =  $("body .reference").val();
                formData.append("brand", brand);
                formData.append("reference", reference);
            }),
        
           
         myDropzone.processQueue();
    });
    
    
    myDropzone.on("success", function(file,response) {

        console.log(response.img);
        $('#imgResponse').html(response.img);
    });

我检查过

myDropzone.on("success", function(file,response) {

        console.log(response.img);
        $('#imgResponse').html(response.img);
    });

在上面的函数中我能够运行提醒,所以成功事件有效但是

console.log(response.img);

不工作。 它工作正常,但只是响应不正确,因此它在控制台中给出未定义的消息。相同的代码在一台服务器上运行良好,而另一台服务器上出现此错误。 我还检查了服务器中是否启用了 json 模块,并使用示例代码进行了编码和解码测试。我还检查了显示在 phpinfo() 中启用。我不明白为什么它不能在这个服务器上工作。如果有人可以帮助我解决这个难题,请告诉我。

谢谢!

确保两台服务器都接收到您的文件。看起来服务器上有错误,您的文件没有上传,这就是您收到空响应的原因

请从 PHP 脚本中注释掉以下 2 行。

  1. file_put_contents('abc.txt',$data);
  2. header('Content-type: application/json');

并将error_reporting(E_ALL);替换为error_reporting(0); 它将关闭来自服务器响应的警告消息。