求教:关于文件下载

stan23 2003-08-19 09:34:23
我想实现下面的功能:
某目录下已经存在文件"file1.rar",做一个"download.php",通过它下载"file1.rar",同时将下载名改为"file2.rar"。就是说,点击"download.php"时,提示“文件保存为‘file2.rar'”。

为了减少服务器负担,不能用PHP先读出file1.rar的内容在输出,而要直接连接。如果象下面这样,文件太大的话就不理想了:
Header("Content-type: application/octet-stream");
Header("Content-Disposition: attachment; filename=file2.rar");
echo $content_of_file1_rar;
...全文
39 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
indeed 2003-08-19
  • 打赏
  • 举报
回复
<?
$upload_dir="../../upload/";
$upload_file = "file1.rar";
$fp = fopen($upload_dir.$upload_file,"r"); // 打开文件
// 输入文件标签
Header("Content-type: application/octet-stream");
Header("Accept-Ranges: bytes");
Header("Accept-Length: ".filesize($upload_dir.$upload_file));
Header("Content-Disposition: attachment; filename=" . $upload_file);
// 输出文件内容
echo fread($fp,filesize($upload_dir . $upload_file));
fclose($fp);
?>
xuzuning 2003-08-19
  • 打赏
  • 举报
回复
copy("file1.rar","file2.rar");
header("Location:file2.rar");
xuzuning 2003-08-19
  • 打赏
  • 举报
回复
copy("file1.rar","file2.rar");
header("Location: file2.rar");
stan23 2003-08-19
  • 打赏
  • 举报
回复
不过我想这种方式还是PHP先把文件内容读到内存中,然后输出~
我希望是类似“location:file1.rar”这种,文件传输时就不需要PHP支持了~
babystudio 2003-08-19
  • 打赏
  • 举报
回复
直接连接?那只有在下载前把文件更名、或完全拷贝一份后更名,然后
header("Location:file2.rar");
这样最快了
sshwsfc 2003-08-19
  • 打赏
  • 举报
回复
看看这样行不?

<?php
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=apache2_cli.rar");
//这里的filename为显示在WEB客户端的文件名。
header("Content-Description: PHP4 Generated Data");
readfile('apache2.rar');
//这里的apache2.rar为在服务器上的文件名。
?>
stan23 2003-08-19
  • 打赏
  • 举报
回复
上面的办法我看都可行,不过都是先用PHP将文件内容读出~
算了,结贴吧~
babystudio 2003-08-19
  • 打赏
  • 举报
回复
运算不走内存行吗?关键在于你对内存的运用是否合理、高效而已

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧