【问题标题】:Json Encode working in one server and on another giving error undefinedJson Encode 在一台服务器上工作,而在另一台服务器上则给出未定义的错误
【发布时间】:2020-12-10 06:20:01
【问题描述】:

我有使用 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() 中启用的显示。我不明白为什么它不能在这个服务器上工作。让我知道是否有人可以帮助我解决这个难题。

谢谢!

【问题讨论】:

    标签: javascript php jquery json dropzone.js


    【解决方案1】:

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

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

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

    【讨论】:

    • 是的!这就是问题所在,它已修复。谢谢!
    【解决方案2】:

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

    【讨论】:

    • 两个文件或在同一台服务器上。我是说所有代码都可以在一台服务器上运行而没有任何错误,但是它不能在另一台服务器上运行。我没有在不同的服务器上发送文件。谢谢!
    • @RiyaShah,您需要确保您上传的文件正在发送到服务器。您在代码中有一个条件,即如果没有收到文件,则不会有响应,并且在客户端 response.img 将是 undefined。只需在正在工作的服务器和不工作的服务器上打印_r $_FILES。
    • 文件上传中没有任何错误。我已经测试过了,即使我在 $data 中对 img 进行了硬编码,它仍然无法正常工作!
    • 在这种情况下,您可能需要提供更多信息。你能分享这两个请求的响应吗?
    猜你喜欢
    • 1970-01-01
    • 2015-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-20
    • 2020-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多