php 发送文件给客户端

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/737045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:41:45  来源:igfitidea点击:

send a file to client

phpdownloadclienttext-files

提问by Kent Fredric

I want to write a text file in the server through Php, and have the client to download that file.

我想通过Php在服务器中写入一个文本文件,并让客户端下载该文件。

How would i do that?

我该怎么做?

Essentially the client should be able to download the file from the server.

本质上,客户端应该能够从服务器下载文件。

回答by Flavius

This is the best way to do it, supposing you don't want the user to see the real URL of the file.

这是最好的方法,假设您不希望用户看到文件的真实 URL。

<?php
  $filename="download.txt";
  header("Content-disposition: attachment;filename=$filename");
  readfile($filename);
?>

Additionally, you could protect your files with mod_access.

此外,您可以使用 mod_access 保护您的文件。

回答by Kent Fredric

In addition to the data already posted, there is a header you might want to try.

除了已经发布的数据之外,您可能还想尝试一个标题。

Its only a suggestionto how its meant to be handled, and the user agent can chose to ignore it, and simply display the file in the window if it knows how:

它只是一个关于如何处理它的建议,用户代理可以选择忽略它,如果它知道如何在窗口中显示文件:

<?php

 header('Content-Type: text/plain');         # its a text file
 header('Content-Disposition: attachment');  # hit to trigger external mechanisms instead of inbuilt

See Rfc2183for more on the Content-Disposition header.

有关Content-Disposition 标头的更多信息,请参阅Rfc2183

回答by Anthony

PHP has a number of very simplistic, C-like functions for writing to files. Here is an easy example:

PHP 有许多非常简单的、类似于 C 的函数用于写入文件。这是一个简单的例子:

<?php
// first parameter is the filename
//second parameter is the modifier: r=read, w=write, a=append
$handle = fopen("logs/thisFile.txt", "w");

$myContent = "This is my awesome string!";

// actually write the file contents
fwrite($handle, $myContent);

// close the file pointer
fclose($handle);
?>

It's a very basic example, but you can find more references to this sort of operation here:

这是一个非常基本的示例,但您可以在此处找到有关此类操作的更多参考:

PHP fopen

PHP打开

回答by tylerl

If you set the content type to application/octet-stream, the browser will ALWAYS offer file as a download, and will never attempt to display it internally, no matter what type of file it is.

如果您将内容类型设置为 application/octet-stream,浏览器将始终提供文件作为下载,并且永远不会尝试在内部显示它,无论它是什么类型的文件。

<?php
  filename="download.txt";
  header("Content-type: application/octet-stream");
  header("Content-disposition: attachment;filename=$filename");

  // output file content here
?>

回答by To1ne

Just post a link on the site to http://example.com/textfile.php

只需在网站上发布一个链接到http://example.com/textfile.php

And in that PHP file you put the following code:

并在该 PHP 文件中放入以下代码:

<?php
header('Content-Type: text/plain');
print "The output text";
?>

That way you can create the content dynamic (from a database)... Try to Google to oter "Content-Type" if this one is not the one you are looking for.

这样您就可以创建动态内容(从数据库中)...如果这不是您要查找的内容,请尝试使用 Google 搜索其他“内容类型”。