Home > Backend Development > PHP Tutorial > Detailed introduction to the implementation of three file downloads in PHP

Detailed introduction to the implementation of three file downloads in PHP

慕斯
Release: 2023-04-10 08:56:02
forward
2926 people have browsed it

This article will give you a detailed introduction to the implementation of three kinds of file downloads in PHP, which has certain reference value. Friends in need can refer to it. I hope it helps everyone.

Detailed introduction to the implementation of three file downloads in PHP

1. Add the file link directly

<button>
    <a href = "http://localhost/down.zip">
    下载文件</button>
Copy after login

Click the button to download :
Detailed introduction to the implementation of three file downloads in PHP

2. Pass parameters to search and jump to the download link

Pass parameters:

<button>
    <a href = "http://localhost?f=&#39;down&#39;">
    下载文件
</button>
Copy after login

Find the file and challenge to the download link:

<?php

$down = $_GET[&#39;f&#39;];   //获取文件参数
$filename = $down.&#39;.zip&#39;; //获取文件名称
$dir ="down/";  //相对于网站根目录的下载目录路径
$down_host = $_SERVER[&#39;HTTP_HOST&#39;].&#39;/&#39;; //当前域名


//判断如果文件存在,则跳转到下载路径
if(file_exists(__DIR__.&#39;/&#39;.$dir.$filename)){
    header(&#39;location:http://&#39;.$down_host.$dir.$filename);
}else{
    header(&#39;HTTP/1.1 404 Not Found&#39;);
}
Copy after login

Result:

  • File Exists

Detailed introduction to the implementation of three file downloads in PHP

  • File does not exist
    Detailed introduction to the implementation of three file downloads in PHP

3. The head() and fread() functions output the file directly to the browser

<?php  
$file_name = "down";$file_name = "down.zip";     //下载文件名    $file_dir = "./down/";        //下载文件存放目录    //检查文件是否存在    if (! file_exists ( $file_dir . $file_name )) {    
    header(&#39;HTTP/1.1 404 NOT FOUND&#39;);  
} else {    
    //以只读和二进制模式打开文件   
    $file = fopen ( $file_dir . $file_name, "rb" ); 

    //告诉浏览器这是一个文件流格式的文件    
    Header ( "Content-type: application/octet-stream" ); 
    //请求范围的度量单位  
    Header ( "Accept-Ranges: bytes" );  
    //Content-Length是指定包含于请求或响应中数据的字节长度    
    Header ( "Accept-Length: " . filesize ( $file_dir . $file_name ) );  
    //用来告诉浏览器,文件是可以当做附件被下载,下载后的文件名称为$file_name该变量的值。
    Header ( "Content-Disposition: attachment; filename=" . $file_name );    

    //读取文件内容并直接输出到浏览器    
    echo fread ( $file, filesize ( $file_dir . $file_name ) );    
    fclose ( $file );    
    exit ();    
}
Copy after login

The result: the same as the second one

Summary: The first and second operations are relatively simple, but they are easy to expose the real address of the file and are not very secure. The third operation can better hide the real address of the file

Recommended learning: php video tutorial

The above is the detailed content of Detailed introduction to the implementation of three file downloads in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template