简体   繁体   English

如何使用php写文件的响应

[英]How to write response to file using php

I wish to write the response of hitting a given url into the href attribute of an anchor tag using PHP. 我希望使用PHP编写将命中url命名为锚标记的href属性的响应。 How can I do this? 我怎样才能做到这一点?

Here's an example of what I excpect to happen 这是我想要发生的事情的一个例子

mylink.com/getdoc?name=documentA

returns a string as a response: 返回一个字符串作为响应:

mylink.com/document2012-03-15.pdf

I need to write this response (using PHP into the href attribute as shown below: 我需要编写这个响应(使用PHP进入href属性,如下所示:

<a href="mylink.com/document2012-03-15.pdf"> Open Document A </a>

(so the above will be the final source of my page. (所以上面将是我页面的最终来源。

I think there are a few ways to do what you want. 我认为有几种方法可以做你想要的。 Not all of them will work exactly as you ask for, but the end result should be the same. 并非所有这些都能按照您的要求完全正常工作,但最终结果应该是相同的。

Solution one 解决方案一

My first possible solution was already posted by @shanethehat. 我的第一个可能的解决方案已由@shanethehat发布。 You could use file_get_contents to call your PHP script via HTTP and get the response. 您可以使用file_get_contents通过HTTP调用PHP脚本并获取响应。

Solution two 解决方案二

Another possible solution was suggested in the comments of the post by @YourCommonSense. @YourCommonSense在帖子的评论中提出了另一种可能的解决方案。 You could simply include the getdoc script in the PHP script that is generating your HTML file, like this: 您可以简单地在生成HTML文件的PHP脚本中include getdoc脚本,如下所示:

$_GET["name"] = "documentA";
echo "<a href=\"". include("getdoc.php") ."\"> Open Document A </a>";

Solution three 解决方案三

Or you could change the way the getdoc script works. 或者您可以更改getdoc脚本的工作方式。 You could use a script more like this: 您可以使用更像这样的脚本:

header("Content-type:application/pdf");
header("Content-Disposition:attachment; filename=\"{$_GET["name"]}\"");
readfile($_GET["name"]);

And you keep your link like this: <a href="mylink.com/getdoc.php?name=documentA"> Open Document A </a> . 并保持这样的链接: <a href="mylink.com/getdoc.php?name=documentA"> Open Document A </a> When getdoc.php is called, it will get the specified file and start a file download. 调用getdoc.php ,它将获取指定的文件并启动文件下载。

NOTE: you should probably do some input sanitization with this method (removing slashes, making sure the file ends in .pdf, etc) to make sure someone doesn't try to get a file they're not allowed to get. 注意:您应该使用此方法进行一些输入清理(删除斜杠,确保文件以.pdf结尾等),以确保某人不会尝试获取他们不允许获取的文件。



That's all I'm coming up with at the moment. 这就是我现在想出来的全部内容。 There might be a more clever way to do it, but hopefully one of these solutions will do it for you. 可能有一种更聪明的方法,但希望其中一种解决方案能够为您完成。 I would try solution 2 or 3 first, and if they don't work out for you, then go with solution 1. 我会首先尝试解决方案2或3,如果它们不适合你,那么请使用解决方案1。

<?php
    //get output from URL
    $myfile = file_get_contents('http://mylink.com/getdoc?name=documentA');
?>
<a href="<?php echo $myfile; ?>">Open Document A</a>

How to write response to file using php 如何使用php写文件的响应

Noway. 没门。
PHP do not process HTTP requests. PHP不处理HTTP请求。
You have to set up your web server to do the rewrite. 您必须设置Web服务器才能进行重写。
There are 100500 questions under mod_rewrite tag, you will find the solution easily. mod_rewrite标签下有100500个问题,您可以轻松找到解决方案。

Note that you may wish to rewrite your url to /getdoc.php?name=document2012-03-15.pdf , not one you mentioned in your question 请注意,您可能希望将您的网址重写为/getdoc.php?name=document2012-03-15.pdf ,而不是您在问题中提到的网址

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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