PHP implements ...LOGIN

PHP implements file upload and download file download

File download

If the browser does not recognize it, you can download it directly, but if it can recognize it, you need to perform some operations

The code is as follows:

<?php
//获取传递过来的路径信息
$filename=$_GET['filename'];
//判断是否有值,没有则不执行下面的php语句
if($filename){
	header("Content-Disposition:attachment;filename=download_$filename");
	//Content-disposition 是 MIME 协议的扩展,MIME 协议指示 MIME 用户代理如何显示附加的文件。
	//格式:content-disposition = "Content-Disposition" ":" disposition-type *( ";" disposition-parm 
	//Content-Disposition为属性名
    //disposition-type是以什么方式下载,如attachment为以附件方式下载
     //disposition-parm为默认保存时的文件名 
	readfile($filename);
	exit;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"> 
 <meta name="format-detection" content="telephone=no" /> 
<title>文件下载</title>
<meta charset="utf-8" />
</head>
<body>
<a href="1.rar">下载1.rar</a>
<br />
<a href="1.jpg">下载1.jpg</a>
<br />
<a href="download.php?filename=1.jpg">通过程序下载1.jpg</a>
</body>
</html>

Note: During testing, 1.rar and 1.jpg must exist in the same directory of the file, or you can modify the path , change to the folder where you want your file


##Code explanation

There are three download links

  • One corresponding picture (can be recognized by the browser)

  • A corresponding compressed file (not recognized by the browser)

  • The last one is still a picture. We pass the path to the current page and open it as an attachment in the php code at the top of the page. Or save

  • The php code part has been commented in detail in the program, you can refer to

<?php //获取传递过来的路径信息 $filename=$_GET['filename']; //判断是否有值,没有则不执行下面的php语句 if($filename){ header("Content-Disposition:attachment;filename=download_$filename"); //Content-disposition 是 MIME 协议的扩展,MIME 协议指示 MIME 用户代理如何显示附加的文件。 //格式:content-disposition = "Content-Disposition" ":" disposition-type *( ";" disposition-parm  //Content-Disposition为属性名 //disposition-type是以什么方式下载,如attachment为以附件方式下载 //disposition-parm为默认保存时的文件名 readfile($filename); exit; } ?> <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"> <meta name="format-detection" content="telephone=no" /> <title>文件下载</title> <meta charset="utf-8" /> </head> <body> <a href="1.rar">下载1.rar</a> <br /> <a href="1.jpg">下载1.jpg</a> <br /> <a href="download.php?filename=1.jpg">通过程序下载1.jpg</a> </body> </html>
submitReset Code
ChapterCourseware