【问题标题】:file_get_contents not working on production server, fine on localfile_get_contents 在生产服务器上不起作用,在本地很好
【发布时间】:2011-10-07 11:22:16
【问题描述】:

我有一个从远程服务器获取图像的 PHP 脚本,以便我可以使用 HTML5 画布 API 对其进行操作。

<?php
if ((isset($_GET['url']))) {
    $url = $_GET['url'];
    $file_format = pathinfo($url, PATHINFO_EXTENSION);
    try
    {   
        header("Content-Type: image/$file_format");
        header("Content-disposition: filename=image.$file_format");
        $img = file_get_contents($url);
        echo $img;
    }

    catch(Exception $e)
    {
        echo $e->getMessage();
    }
}

else die('Unknown request');
?>

一个典型的请求如下所示:

fetch_image.php?url=http://example.com/images/image.png

在我的本地服务器上一切正常,但生产服务器给了我这个错误:

NetworkError: 500 内部服务器错误。

错误日志记录了这条消息:

PHP 警告:无法修改标头信息 - 标头已发送。

我已经尝试了一些建议,但没有奏效:

allow_url_fopen = 1

【问题讨论】:

  • 请粘贴完整的脚本(包括&lt;?php标签)。
  • 听起来像是走错路了
  • 错误指向哪几行?另请发布确切的错误消息。
  • 如果您只是在远程服务器上呈现图像而无需额外处理,为什么不直接重定向到该图像?
  • @Meouw - HTML5 canvas api 不允许您操作来自另一个域的图像的像素。

标签: php file-get-contents production-environment


【解决方案1】:

检查服务器是否允许您使用文件函数打开远程 URL(php.ini "allow_url_fopen" 设置必须为 "true")。

【讨论】:

    【解决方案2】:

    试试

    ob_start()
    

    在开头和

    ob_end_flush()
    

    在脚本的末尾。还要确保脚本在 &lt;?php 之前不包含任何字符。

    【讨论】:

      【解决方案3】:

      您应该确保您的托管服务提供商没有出于安全原因禁用远程 URL 获取。设置为allow_url_fopen,您可以使用phpinfo() 检查当前配置。在这种情况下,file_get_contents() 应该返回 FALSE,因此您必须使用 === 运算符测试 $img 是否为 false。

      【讨论】:

        【解决方案4】:

        试试这个方法

        <?php
        if ((isset($_GET['url']))) {
            $url = $_GET['url'];
            $file_format = pathinfo($url, PATHINFO_EXTENSION);
            try
            {   
                ob_clean();
                ob_start();
        
                header("Content-Type: image/$file_format");
                header("Content-disposition: filename=image.$file_format");
                $img = file_get_contents(urlencode($url));
               // as per manual "If you're opening a URI with special characters, such as spaces, you need to encode the URI with urlencode(). " 
                echo $img;
                echo ob_get_clean();
                exit();
            }
        
            catch(Exception $e)
            {
                echo $e->getMessage();
            }
        }
        
        else die('Unknown request');
        ?>
        

        来自manual的另一个解决方案

        有时您可能会在打开 http URL 时遇到错误。 即使您在 php.ini 中设置了“allow_url_fopen = On”

        对我来说,解决方案是将“user_agent”也设置为某个值。

        【讨论】:

          【解决方案5】:

          当您开始输出内容时会发送标头。因此,在您上面提供的代码之前的某个地方,内容会被回显(来自 PHP 或纯 HTML 或 javascript)。你需要找出发生这种情况的地方。

          【讨论】:

            【解决方案6】:
            1. 您应该检查文件的编码,因为 utf8 会破坏标题
            2. 在运行脚本之前检查您的文件是否不打印任何其他数据。

            【讨论】:

              猜你喜欢
              • 2012-11-15
              • 2013-10-02
              • 1970-01-01
              • 2016-05-15
              • 1970-01-01
              • 1970-01-01
              • 2011-07-31
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多