简体   繁体   中英

Json Working on Localhost but not on server

I installed a server (VPS) and made sure that JSON plugin is enabled. Now, I'm using Laravel and one helper file returns the code list of files. It's working on Localhost, but not working on HTTPS server.

Now, this echo json_encode($enteries) is working on localhost (MAMP) but not working on liver server. I am using Laravel V5.2 I get responset type: text/html in response on server. Whereas on localhost, it comes to be application/json

Thank you in advance.

<?php
namespace ImageBrowser;
use ImageBrowserEntry\ImageBrowserEntry;
use Thumbnail\Thumbnail;

class ImageBrowser {
    // path to file upload directory
    private $contentPath = '';

    public function __construct()
    {
        $this->contentPath = public_path() .\Config::get('global.DIRECTORY_SEPERATOR'). \Config::get('global.image_browser_path');
    }

    private function canAccess($path) {
        return \ImageHelper::startsWith(realpath($path), realpath($this->contentPath));
    }

    private function ensureAccess($path) {
        if (!$this->canAccess($path)) {
            header('HTTP/1.0 403 Forbidden');
            die();
        }
    }

    private function normalize($path) {
        if (!\ImageHelper::endsWith($path, '/')) {
            $path .= '/';
        }

        return $path;
    }

    public function basePath() {
        return $this->normalize($this->contentPath);
        //return $this->normalize(realpath(dirname(__FILE__) . $this->contentPath));
    }

    public function getList($path) {
        $this->ensureAccess($path);

        header('Content-Type: application/json');

        $dir = array_map(function ($scan_entry) use ($path) {
            if (\ImageHelper::startsWith($scan_entry, '.')) {
                return;
            }

            $entry = new ImageBrowserEntry();

            $fullpath = realpath($path . $scan_entry);

            $entry->name = $scan_entry;
            $entry->type = is_dir($fullpath) ? 'd' : 'f';
            $entry->size = filesize($fullpath);

            if ($entry->type == 'f' && preg_match('/\\.(png|gif|jpg|jpeg)$/i', $scan_entry) == 0) {
                return;
            }

            return $entry;
        }, scandir($path));

        $entries = array();

        foreach ($dir as $entry) {
            if ($entry) {
                $entries[] = $entry;
            }
        }



        echo json_encode($entries);
    }

    public function setImageHeaders($path, $type=null) {
        if (!$type) {
            $type = \ImageHelper::getImageType($path);
        }

        header("Content-type: image/" . $type);
        header("Expires: Mon, 1 Jan 2099 05:00:00 GMT");
        header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
        header("Cache-Control: no-store, no-cache, must-revalidate");
        header("Cache-Control: post-check=0, pre-check=0", false);
        header("Pragma: no-cache");

        // get the size for content length
        $size = filesize($path);
        header("Content-Length: $size bytes");
        if (ob_get_contents()) ob_end_clean();
        //ob_clean();
        flush();
    }

    public function getThumbnail($path) {
        $this->ensureAccess($path);

        $image = new Thumbnail($path);

        $this->setImageHeaders($path, $image->getType());

        $image->downscale();
        $image->render();
    }

    public function getImage($path) {
        $this->ensureAccess($path);

        $this->setImageHeaders($path);

        readfile($path);
    }

    public function destroy($path, $entry) {
        $target = $this->normalize($path) . $entry;

        $this->ensureAccess($target);

        if (is_dir($target)) {
            \ImageHelper::rmdir_r($target);
        } else {
            unlink($target);
        }
    }

    public function create($path, $entry) {
        $this->ensureAccess($path);

        mkdir($path . $entry);
    }

    public function saveFile($file, $path) {
        $path = $this->normalize($path);

        $this->ensureAccess($path);

        $name = basename($file['name']);

        $target = $path . $name;

        move_uploaded_file($file['tmp_name'], $target);

        header('Content-Type: application/json');

        $result = new ImageBrowserEntry();
        $result->size = filesize($target);
        $result->name = $name;

        echo json_encode($result);
    }
}

Found Answer by myself. The Laravel env. is strange and all I changed was remove native PHP object methods and then it worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM