PHP fopen:无法打开流:成功 +如何将获取的内容保存到文件中?

发布于 2024-09-30 14:08:36 字数 2522 浏览 6 评论 0原文

我编写了一个“proxy.php”脚本(如下所列),它将获取 ?img= 参数中指定的图像并将其打印到 STDOUT。我的 Flash 应用程序需要这样做才能避免某些站点缺少 crossdomain.xml。

它有效,但我有 3 个问题。另外,我是从 Perl 转向 PHP 的,但我的 PHP 知识仍然有很多差距(但我意识到,stream_context_create 和 fpassthru 可能使用桶队列)。

1) 在我的 callback() 函数中,如何将调试消息打印到 PHP 日志? (它被重定向到我的 CentOS 机器上的 /var/log/messages)

2) 为什么我收到错误消息 无法打开流:成功,我是否错过了 回调中的情况() 或许?

PHP Warning:  fopen() [<a href='function.fopen'>function.fopen</a>]: php_network_getaddresses: getaddrinfo failed: Name or service not known in /var/www/html/proxy.php on line 19
PHP Warning:  fopen(http://i136.odnoklassniki.ru/getImage?photoId=154105499212&amp;photoType=0) [<a href='function.fopen'>function.fopen</a>]: failed to open stream: Success in /var/www/html/proxy.php on line 19

3) 因为我的脚本经常使用相同的图像 URL 作为参数来调用,所以我想扩展它,以便它在第一次调用时将获取的文件保存在目录中。在第一次和后续调用时,它应该将该缓存文件提供给 STDOUT。您有什么建议,如何以节省内存的方式进行制作吗?即我不想使用 get_file_contents() 立即读取整个文件

<?php

define('MAX_SIZE', 1024 * 1024);

$img = urldecode($_GET['img']);
if (strpos($img, '..') !== FALSE)
        exit('Wrong URL: ' . $img);

$opts = array(
        'http'=>array(
                'method' => 'GET'
        )
);

$ctx = stream_context_create($opts);
stream_context_set_params($ctx, array('notification' => 'callback'));
$fh = fopen($img, 'r', FALSE, $ctx);
if ($fh) {
        fpassthru($fh);
        fclose($fh);
}

function callback($code, $severity, $message, $message_code, $bytes_transferred, $bytes_total) {
        if ($code == STREAM_NOTIFY_PROGRESS && $bytes_transferred > MAX_SIZE)
                exit('File is too big: ' . $bytes_transferred);

        if ($code == STREAM_NOTIFY_FILE_SIZE_IS)
                if ($bytes_total > MAX_SIZE)
                        exit('File is too big: ' . $bytes_total);
                else
                        header('Content-Length: ' . $bytes_total);

        if ($code == STREAM_NOTIFY_MIME_TYPE_IS) {
                if (stripos($message, 'image/gif') !== FALSE ||
                    stripos($message, 'image/png') !== FALSE ||
                    stripos($message, 'image/jpg') !== FALSE ||
                    stripos($message, 'image/jpeg') !== FALSE) {
                        header('Content-Type: ' . $message);
                } else {
                        exit('File is not image: ' . $mime);
                }
        }
}

?>

I have programmed a "proxy.php" script (listed below), which would fetch an image specified in the ?img= parameter and print it to the STDOUT. This is needed for my Flash app to circumvent a missing crossdomain.xml at some sites.

It works, but I have 3 questions please. Also I'm coming to PHP from Perl and still have many gaps in my PHP-knowledge (but I realize, that stream_context_create and fpassthru probably use bucket brigades).

1) In my callback() function, how can I print debugging messages to PHP log? (it is redirected to /var/log/messages at my CentOS machine)

2) Why do I get the error message failed to open stream: Success, do I miss a case in the callback() maybe?

PHP Warning:  fopen() [<a href='function.fopen'>function.fopen</a>]: php_network_getaddresses: getaddrinfo failed: Name or service not known in /var/www/html/proxy.php on line 19
PHP Warning:  fopen(http://i136.odnoklassniki.ru/getImage?photoId=154105499212&photoType=0) [<a href='function.fopen'>function.fopen</a>]: failed to open stream: Success in /var/www/html/proxy.php on line 19

3) Because my script is often called with the same image URL as parameter, I would like to extend it so that it saves the fetched file in a dir on the 1st call. And on the 1st and subsequent calls it should serve that cached file to STDOUT. Do you have a suggestion, how to make it in a memory-conserving way? I.e. I don't want to read the whole file at once with get_file_contents()

<?php

define('MAX_SIZE', 1024 * 1024);

$img = urldecode($_GET['img']);
if (strpos($img, '..') !== FALSE)
        exit('Wrong URL: ' . $img);

$opts = array(
        'http'=>array(
                'method' => 'GET'
        )
);

$ctx = stream_context_create($opts);
stream_context_set_params($ctx, array('notification' => 'callback'));
$fh = fopen($img, 'r', FALSE, $ctx);
if ($fh) {
        fpassthru($fh);
        fclose($fh);
}

function callback($code, $severity, $message, $message_code, $bytes_transferred, $bytes_total) {
        if ($code == STREAM_NOTIFY_PROGRESS && $bytes_transferred > MAX_SIZE)
                exit('File is too big: ' . $bytes_transferred);

        if ($code == STREAM_NOTIFY_FILE_SIZE_IS)
                if ($bytes_total > MAX_SIZE)
                        exit('File is too big: ' . $bytes_total);
                else
                        header('Content-Length: ' . $bytes_total);

        if ($code == STREAM_NOTIFY_MIME_TYPE_IS) {
                if (stripos($message, 'image/gif') !== FALSE ||
                    stripos($message, 'image/png') !== FALSE ||
                    stripos($message, 'image/jpg') !== FALSE ||
                    stripos($message, 'image/jpeg') !== FALSE) {
                        header('Content-Type: ' . $message);
                } else {
                        exit('File is not image: ' . $mime);
                }
        }
}

?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁许谁一生繁华 2024-10-07 14:08:36
  1. 因为:

    sviss@sviss:~$ 主机 i136.odnoklassniki.ru
    未找到主机 i136.odnoklassniki.ru:3(NXDOMAIN)
    
  1. Because:

    sviss@sviss:~$ host i136.odnoklassniki.ru
    Host i136.odnoklassniki.ru not found: 3(NXDOMAIN)
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文