Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/426.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Json编码在一台服务器和另一台服务器上工作,给出未定义的错误_Javascript_Php_Jquery_Json_Dropzone.js - Fatal编程技术网

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

Javascript Json编码在一台服务器和另一台服务器上工作,给出未定义的错误,javascript,php,jquery,json,dropzone.js,Javascript,Php,Jquery,Json,Dropzone.js,我有非常基本的文件上传系统使用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.

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

}
// 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脚本下面的两行

  • 文件内容('abc.txt',$data)
  • header('Content-type:application/json')
  • 并替换
    错误报告(E_ALL)
    错误报告(0)
    
    它将关闭服务器响应中的警告消息。

    请在PHP脚本的下面两行注释掉

  • 文件内容('abc.txt',$data)
  • header('Content-type:application/json')
  • 并替换
    错误报告(E_ALL)
    错误报告(0)
    
    它将关闭服务器响应中的警告消息。

    两个文件或同一服务器上的文件。我的意思是,所有这些代码都可以在一台服务器上正常工作,没有任何错误,但是它不能在另一台服务器上工作。我没有在不同的服务器上发送文件。谢谢@RiyaShah您需要确保您正在上载的文件正在发送到服务器。您在代码中有一个条件,即如果没有收到任何文件,将不会有响应,并且在客户端
    response.img
    将是
    未定义的
    。只需在正在工作的服务器和未工作的服务器上打印\u r$\u文件。文件已上载,并且没有任何错误。我已经测试过了,即使我用$data硬编码img,它仍然不工作!在这种情况下,您可能需要提供更多信息。您可以共享两个请求的响应吗?两个文件还是在同一台服务器上。我的意思是,所有这些代码都可以在一台服务器上正常工作,没有任何错误,但是它不能在另一台服务器上工作。我没有在不同的服务器上发送文件。谢谢@RiyaShah您需要确保您正在上载的文件正在发送到服务器。您在代码中有一个条件,即如果没有收到任何文件,将不会有响应,并且在客户端
    response.img
    将是
    未定义的
    。只需在正在工作的服务器和未工作的服务器上打印\u r$\u文件。文件已上载,并且没有任何错误。我已经测试过了,即使我用$data硬编码img,它仍然不工作!在这种情况下,您可能需要提供更多信息。你能分享两个请求的响应吗?是的!这就是问题所在,已经解决了。谢谢是 啊这就是问题所在,已经解决了。谢谢