我的服务器上有一个CSV文件.如果用户点击了应该下载的链接,而是在我的浏览器窗口中打开它.
我的代码如下所示
<a href="files/csv/example/example.csv">
Click here to download an example of the "CSV" file
</a>
Run Code Online (Sandbox Code Playgroud)
这是一个普通的网络服务器,我可以完成所有的开发工作.
我尝试过类似的东西:
<a href="files/csv/example/csv.php">
Click here to download an example of the "CSV" file
</a>
Run Code Online (Sandbox Code Playgroud)
现在我的csv.php文件的内容:
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename=example.csv');
header('Pragma: no-cache');
Run Code Online (Sandbox Code Playgroud)
现在我的问题是它正在下载,但不是我的CSV文件.它会创建一个新文件.
有人可以保存这两个文件并运行它们并告诉我为什么我收到错误"ob_flush()[ref.outcontrol]:无法刷新缓冲区.没有缓冲区要刷新".我试着用Google搜索,它说我必须使用ob_start(); 但是当我这样做时,它不会逐行打印,而是在完成后从FOR循环返回整个对象.我是PHP的新手,所以我不知道还有什么地方要看..
test_process.php
// This script will write numbers from 1 to 100 into file
// And sends continuously info to user
$fp = fopen( '/tmp/output.txt', 'w') or die('Failed to open');
set_time_limit( 120);
ignore_user_abort(true);
for( $i = 0; $i < 100; $i++){
echo "<script type=\"text/javascript\">parent.document.getElementById( 'foo').innerHTML += 'Line $i<br />';</script>";
echo str_repeat( ' ', 2048);
flush();
ob_flush();
sleep(1);
fwrite( $fp, "$i\n");
}
fclose( $fp);
Run Code Online (Sandbox Code Playgroud)
main.html中
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript" charset="utf-8"></script>
<style type="text/css" media="screen">
.msg{ background:#aaa;padding:.2em; border-bottom:1px #000 solid}
.new{ …Run Code Online (Sandbox Code Playgroud) 我必须触发一个zip文件的下载(Zip文件在我的数据文件夹中).为此,我正在使用代码,
$file = 'D:\php7\htdocs\Project\trunk\api\data\file.zip';
header('Content-Description: File Transfer');
header('Content-type: application/zip');
header('Content-disposition: attachment; filename=' . basename($file) );
readfile($file);`
Run Code Online (Sandbox Code Playgroud)
这正在我的预期核心PHP中工作.但是当我在Zend中使用相同的代码时,打印出如下内容,
PKYsVJ)~?? study.xlsPKYsVJs?????+
tutorial-point-Export.xlsPKYsVJn??? 8??Zabc.xlsP
在内容之间我可以看到zip中所有文件的名称.但它没有下载.
在我意识到这不起作用之后我开始搜索它并从堆栈流程中找到一些解决方案
尝试1:ob在每个随机行中添加不同的标题元素和函数
header('Content-Transfer-Encoding: binary');header('Expires: 0');header('Cache-Control: must-revalidate, post-check=0, pre-check=0');header('Pragma: public');header('Content-Length: ' . $file_size);ob_start();ob_clean();flush();所有这些都是从不同的堆栈溢出问题和答案尝试并具有相同的结果
尝试2:PHP正在读取文件而不是下载.这个问题没有任何公认的答案(他在询问核心php,但我只有zend的问题).我尝试了所有这些,但它没有用.
尝试3:更改.htaccess.之后我认为这是我的.htaccess的一个问题,并找到了更改.htaccess文件的答案.
<FilesMatch "\.(?i:zip)$">
ForceType application/octet-stream
Header set Content-Disposition attachment
</FilesMatch>
Run Code Online (Sandbox Code Playgroud)
这也给了我相同的结果.
尝试4:在Zend中使用下载功能.我在这个问题的答案中尝试了所有的zend函数.但是给我一个空输出,即使文件没有被读取.
尝试5:根据答案删除php标记之前和之后的所有不需要的空格
有没有其他方法可以在ZF2框架中触发下载?
编辑
以下是我的确切功能.这是GET(API)函数,
public function getList(){
try{
//here i am getting the zip …Run Code Online (Sandbox Code Playgroud) 只是想问一下如何阻止Web浏览器在浏览器上查看,因为每次用户点击链接下载它在浏览器中查看
asp.net controller
<li><asp:HyperLink ID="hl_download" runat="server" >Download</asp:HyperLink></li>
html
<a id="dnn_ctr932_View_hl_download" href="/ideaPark/DesktopModules/ResourceModule/pdf_resources/IdeaPark_ER_diagram.pdf">Download</a>
Run Code Online (Sandbox Code Playgroud) 我知道有关于使用Laravel 下载文件的问题 ,但我发现只处理本地文件.
所以,通常情况下,我会这样做:
$path = storage_path().'/my-file.pdf';
$name = 'new-name.pdf';
$headers = '....';
return response()->download($path, $name, $headers);
Run Code Online (Sandbox Code Playgroud)
但是,当我需要下载的文件是外部文件时,我该怎么做,存放在www.my-storage.net/files/123/my-file.pdf下?
我可以使用copy()函数,或者file_get_contents()和Laravel的Storage :: put()的组合,但我不想在每次下载处理时本地(和临时)存储文件.相反,我想直接通过用户的浏览器下载外部文件.
在普通的PHP中,它会是这样的.但...
(1)如何准备这样的Laravel下载响应?
(2)如何确保Laravel解决方案保持高效(例如,没有内存泄漏)?
我想强迫用户下载YouTube视频.例如这个网址.我下载视频,我可以播放原始视频,但即使是视频的长度/大小也是相同的,我在强制下载时无法播放.
function force_download($file,$video_url)
{
$video_data = file_get_contents($video_url);
file_put_contents($file, $video_data);
if(isset($file) && file_exists($file))
{
header('Content-length: ' . filesize($file));
header('Content-type: application/octet-stream');
header('Content-Disposition: attachment; filename= "' . $file . '"');
readfile($file);
}
}
force_download('youtube.mp4',$video_url);
Run Code Online (Sandbox Code Playgroud) 我有一些代码使用PHP标题下载文件,但它没有正常工作,并希望添加目录阅读
<?php
if(isset($_GET['link'])){
$var_1 = $_GET['link'];
$dir='/upload/';
}
?>
<?php
if(isset($_GET['link'])){
$var_1 = $_GET['link'];
$file = $var_1;
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
echo "<h1>Content error</h1><p>The file does not exist!</p>";
}
?>
Run Code Online (Sandbox Code Playgroud)
它显示错误内容错误
该文件不存在!
我在用
这个链接下载文件原始位置是
我最近遇到了“ 如何使用PHP强制下载文件”,其中介绍了设置Content-Transfer-Encoding。当通过HTTP下载文件时,此标头与该应用程序一起使用时是否适用(它似乎与电子邮件相关)?如果是这样,应将MS Word和Excel文件,ZIP文件,PDF和文本设置为什么?
header("Content-Transfer-Encoding: Binary");
Run Code Online (Sandbox Code Playgroud) 我目前正在尝试从FTP服务器检索文件,以便用户可以下载.ftp_get()把它写到本地机器上的路径,是的,但是我想要的是它也出现在下载历史记录中,并且从互联网上算作"正常"下载,但我还没弄清楚如何做到这一点.我还尝试直接链接到PHP中的文件,header("Location: ftp://username:password@ftp.server.com/myfile.file")但这导致浏览器显示文件内容(我不想要).我错过了任何header参数吗?或者有一种完全不同的方式来做到这一点?