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

我有一个非常基本的文件上传系统,使用 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();


}

我已经查过

myDropzone.on("success", function(file,response) { 
       console.log(response.img);
        $('#imgResponse').html(response.img);
    });

在上面的函数中,我能够运行警报,因此成功事件有效,但是

console.log(response.img);

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


慕运维8079593
浏览 74回答 2
2回答

收到一只叮咚

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

动漫人物

请注释掉 PHP 脚本中的以下 2 行。file_put_contents('abc.txt',$data);header('Content-type: application/json');并替换error_reporting(E_ALL);为error_reporting(0);&nbsp;它将关闭服务器响应的警告消息。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript