来自外部服务器的PHP读取文件 [英] PHP readfile from external server

查看:242
本文介绍了来自外部服务器的PHP读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从外部服务器的readfile()遇到问题.下载的文件始终是损坏的,大小约为3,4kb.它正在本地主机上工作.

I've got a problem with readfile() from external server. Downloaded file is always broken and has size about 3,4kb. It's working on local host.

第一:

$file_name = $_POST['myname'];
readfile("http://www.ftj.eu/.../3n.pdf");
header("Content-Disposition: attachment; filename=$file_name" .date("m-d-y") . ".pdf");

第二:

您知道为什么即使在本地主机上它也无法正常工作吗?:

Do you know why it isn't working even on local host?:

readfile("3n.pdf");
header("Content-Disposition: attachment; filename=$_POST['myname']" .date("m-d-y") . ".pdf");

解析错误:语法错误,意外的T_ENCAPSED_AND_WHITESPACE,预期为T_STRING或T_VARIABLE或T_NUM_STRING

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

请帮助我.

帮助我使外部网址的readfile正常工作.

help me make readfile from external url working.

推荐答案

您遇到语法错误,而readfile()并不是问题,因为@Will已经提到.

You have a syntax error, not a problem with readfile() as @Will already mentioned.

替换此:

header("Content-Disposition: attachment; filename=$_POST['myname']" .date("m-d-y") . ".pdf");

以此(在$_POST['myname']周围添加花括号):

With this (adding the curly braces around $_POST['myname']):

header("Content-Disposition: attachment; filename={$_POST['myname']}" .date("m-d-y") . ".pdf");
                                                  ^                ^

对于来自外部URL的readfile(),这是PHP手册对此要说的:

As for readfile() from an external URL, this is what the PHP Manual has to say about it :

如果打开包装器已启用.有关如何指定文件名的更多详细信息,请参见 fopen().请参阅支持的协议和包装器,以获取有关各种包装器具有哪些功能的信息的链接. ,使用说明以及可能提供的任何预定义变量的信息.

A URL can be used as a filename with this function if the fopen wrappers have been enabled. See fopen() for more details on how to specify the filename. See the Supported Protocols and Wrappers for links to information about what abilities the various wrappers have, notes on their usage, and information on any predefined variables they may provide.

这意味着您必须在php.ini中启用 allow_url_fopen 或您可以使用curl在服务器上下载文件,然后将其提供给客户.

What that means is that you have to enable allow_url_fopen in your php.ini or you can use curl to download the file on your server then serve it to your clients.

使用curl的示例:

<?php 
    $remote_file_url = 'http://www.ftj.eu/.../3n.pdf';
    $downloadedFileName = "your_pdf_file.pdf";

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $remote_file_url);
    $downloadedFile = fopen($downloadedFileName, 'w+');
    curl_setopt($ch, CURLOPT_FILE, $downloadedFile);
    curl_exec ($ch);

    curl_close ($ch);
    fclose($downloadedFile);

    readfile($downloadedFileName);
    header("Content-Disposition: attachment; filename={$_POST['myname']}" .date("m-d-y") . ".pdf");

这篇关于来自外部服务器的PHP读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆