【问题标题】:Json Working on Localhost but not on serverJson 在本地主机上工作但不在服务器上
【发布时间】:2017-08-03 17:06:29
【问题描述】:

我安装了服务器 (VPS) 并确保启用了 JSON 插件。 现在,我正在使用 Laravel,一个帮助文件返回文件的代码列表。 它在 Localhost 上工作,但在 HTTPS 服务器上不工作。

现在,这个 echo json_encode($enteries) 在 localhost (MAMP) 上工作,但在肝脏服务器上不工作。 我正在使用 Laravel V5.2 我在服务器上得到 responset type: text/html 作为响应。 而在 localhost 上,它是 application/json

提前谢谢你。

<?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);
    }
}

【问题讨论】:

  • 在浏览器中访问 JSON 端点会显示什么?
  • 你真的应该使用response()-&gt;json() - 你的 Laravel 控制器通常不应该直接回显内容。
  • 这是一个不同的脚本。是的,我尝试了 response()->json()。仍然返回 text/html。
  • 您是返回 response()-&gt;json() 还是回应?你正在做很多绕过内置响应系统的非 Laravel-y 事情。例如,您的 403 错误应该是 abort(403)。标题应该设置在像return response('Lorem ipsum...')-&gt;header('foo', 'bar') 这样的响应上。 response()-&gt;json() 为您处理标题。等等等等等等。

标签: php json


【解决方案1】:

自己找到答案。 Laravel 环境。很奇怪,我所做的只是删除原生 PHP 对象方法,然后它就起作用了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-28
    • 1970-01-01
    相关资源
    最近更新 更多