如何通过PHP代码下载文件 [英] how to download a file through php code
本文介绍了如何通过PHP代码下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在写一个用于下载文件的代码。它正在运行,但它正在下载我的php文件而不是所选文件下载。
我创建了download.php,其中我写了我的代码。下面是我的代码
<?php
if(isset($ _ REQUEST [' download']))
{
// $ path = BASE_URL。/ pdf /;
// $ filename = $ path.basename($ _ GET ['download_file']);
$ filename = $ _ POST [' file'];
header( Cache-Control:public);
header( Content-Description:File Transfer);
header(' Content-disposition:attachment; filename =' .basename($ filename) );
header( Content-Type:application / octet-stream);
header( Content-Transfer-Encoding:binary);
header(' Content-Length:' .filesize($ filename));
readfile($ filename);
退出;
}
?>
< html > < 表格 >
< input type = file name < span class =code-keyword> = file / > < 输入 type =温泉n> 提交 名称 = download 值 = download / > < / form >
。通过这段代码我只是下载我的代码。但是我想下载一个由input type = file浏览的文件。
请帮助
解决方案
_REQUEST [' 下载]))
{
//
path = BASE_URL。/ pdf /;
//
文件名=
I am writing a code in php for downloading a file .It is working but it is downloading my php file rather then the selected file to download.
I have created download.php in which i have written my code.below is my code
<?php
if(isset($_REQUEST['download']))
{
//$path = BASE_URL."/pdf/";
//$filename= $path.basename($_GET['download_file']);
$filename=$_POST['file'];
header("Cache-Control: public");
header("Content-Description: File Transfer");
header('Content-disposition: attachment;filename='.basename($filename));
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: binary");
header('Content-Length: '. filesize($filename));
readfile($filename);
exit;
}
?>
<html><form>
<input type="file" name="file" /><input type="submit" name="download" value="download" /></form>
.By this code i am just downloading my this code only.But i want to download a file which is browsed by input type=file.
Please help
解决方案
_REQUEST['download'])) { //
path = BASE_URL."/pdf/"; //
filename=
这篇关于如何通过PHP代码下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文