为什么我的json_encode在我的网站上不起作用但在localhost中工作?

时间:2017-02-04 07:46:11

标签: php

我有这样的代码,但为什么它不能在网络上工作但在localhost中工作?

<?php
    header('Content-Type: application/json; charset=utf-8');

    include('functions.php');
    /*defined settings - start*/
    ini_set("memory_limit", "99M");
    ini_set('post_max_size', '20M');
    ini_set('max_execution_time', 600);
    define('IMAGE_SMALL_DIR', './uploades/small/');
    define('IMAGE_SMALL_SIZE', 50);
    define('IMAGE_MEDIUM_DIR', './uploades/medium/');
    define('IMAGE_MEDIUM_SIZE', 250);
    define('IMAGE_LARGE_DIR', './foto/');
    define('IMAGE_LARGE_SIZE', 250);
    /*defined settings - end*/

    if(isset($_FILES['image_upload_file'])){
        $output['status']=FALSE;
        set_time_limit(0);
        $allowedImageType = array("image/gif",   "image/jpeg",   "image/pjpeg",   "image/png",   "image/x-png"  );

        if ($_FILES['image_upload_file']["error"] > 0) {
            $output['error']= "Error in File";
        }
        elseif (!in_array($_FILES['image_upload_file']["type"], $allowedImageType)) {
            $output['error']= "You can only upload JPG, PNG and GIF file";
        }
        elseif (round($_FILES['image_upload_file']["size"] / 1024) > 4096) {
            $output['error']= "You can upload file size up to 4 MB";
        } else {
            /*create directory with 777 permission if not exist - start*/
            createDir(IMAGE_SMALL_DIR);
            createDir(IMAGE_MEDIUM_DIR);
            /*create directory with 777 permission if not exist - end*/
            $path[0] = $_FILES['image_upload_file']['tmp_name'];
            $file = pathinfo($_FILES['image_upload_file']['name']);
            $fileType = $file["extension"];
            $desiredExt='jpg';
            $fileNameNew = rand(333, 999) . time() . ".$desiredExt";
            $path[1] = IMAGE_MEDIUM_DIR . $fileNameNew;
            $path[2] = IMAGE_SMALL_DIR . $fileNameNew;

            if (createThumb($path[0], $path[1], $fileType, IMAGE_MEDIUM_SIZE, IMAGE_MEDIUM_SIZE,IMAGE_MEDIUM_SIZE)) {

                if (createThumb($path[1], $path[2],"$desiredExt", IMAGE_SMALL_SIZE, IMAGE_SMALL_SIZE,IMAGE_SMALL_SIZE)) {
                    $output['status']=TRUE;
                    $output['image_medium']= $path[1];
                    $output['image_small']= $path[2];
                }
            }
        }

        echo json_encode($output);

 }
?>  

我对这一切都很累,请帮助我!!!! 我有这样的代码来使图像显示但它不会出现

 $(document).on('change', '#ifile', function () {
    var progressBar = $('.progressBar'), bar = $('.progressBar .bar'), percent = $('.progressBar .percent');

    $('#image_upload_form').ajaxForm({
        beforeSend: function() {
            progressBar.fadeIn();
            var percentVal = '0%';
            bar.width(percentVal)
            percent.html(percentVal);
        },
        uploadProgress: function(event, position, total, percentComplete) {
            var percentVal = percentComplete + '%';
            bar.width(percentVal)
            percent.html(percentVal);
        },

         success: function(html, statusText, xhr, $form) {      
            objy = $.parseJSON(html);   
            if(objy.status){        
                    $("#img8>img").prop('src','objy.image_medium');     

            }else{
                alert(objy.error);
            }
        },
        complete: function(xhr) {


            progressBar.fadeOut();          
        }   
    }).submit();        

    });

1 个答案:

答案 0 :(得分:0)

现场删除$.parseJSON(html)并检查。它应该工作。

success: function(html, statusText, xhr, $form) {      
    if(html.status){        
        $("#img8>img").prop('src','html.image_medium');     
    }else{
        alert(html.error);
    }
},