Home Backend Development PHP Tutorial PHP实现文件安全下载_PHP

PHP实现文件安全下载_PHP

Jun 01, 2016 pm 12:35 PM
fi file download Safety accomplish document

你可能会笑我"下载文件"如此简单都值得说当然并不是想象那么简单。例如你希望客户要填完一份表格才可以下载某一文件你第一个想法一定是用 "Redirect"的方法先检查表格是否已经填写完毕和完整然后就将网址指到该文件这样客户才能下载但如果你想做一个关于"网上购物"的电子商务网站考虑安全问题你不想用户直接复制网址下载该文件笔者建议你使用PHP直接读取该实际文件然后下载的方法去做。程序如下



            $file_name = "tianhys.exee";

            $file_dir = "/public/www/download/";

            if (!file_exists($file_dir . $file_name)) { //检查文件是否存在

           
echo "文件找不到";

            exit;

            } else {

            $file = fopen($file_dir . $file_name,"r"); // 打开文件

           
// 输入文件标签

            Header
("Content-type: application/octet-stream");

            Header("Accept-Ranges: bytes");

            Header("Accept-Length: ".filesize($file_dir . $file_name));

            Header("Content-Disposition: attachment; filename=" .$file_name);

            // 输出文件内容

           
echo fread($file,filesize($file_dir . $file_name));

            fclose($file);

            exit;}





      而如果文件路径是"http" 或者 "ftp" 网址的话则源代码会有少许改变程序如下







            $file_name
= "tianhys.exe";

            $file_dir = "http://www.tianhys.org/";

            $file = @ fopen($file_dir . $file_name,"r");

            if (!$file) {

            echo "文件找不到";

            } else {

            Header("Content-type: application/octet-stream");

            Header("Content-Disposition: attachment; filename=" .$file_name);

            while (!feof ($file)) {

            echo fread($file,50000);

            }

            fclose ($file);

            }

      这样就可以用PHP直接输出文件

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1667
14
PHP Tutorial
1273
29
C# Tutorial
1255
24
How to download meeting minutes from Tencent Conference_How to download meeting minutes from Tencent Conference How to download meeting minutes from Tencent Conference_How to download meeting minutes from Tencent Conference Apr 02, 2024 pm 03:30 PM

1. First open Tencent Meeting and click [History Meeting] on the homepage. 2. After entering the meeting, click [Cloud Recording] to download. 3. Finally, to download the cloud recording video, the meeting host needs to enable the download permission and have the permission to view the recording file.

How to download qq music to USB flash drive? -How to download qq music to USB disk How to download qq music to USB flash drive? -How to download qq music to USB disk Apr 01, 2024 am 10:11 AM

1. Open QQ Music software, enter the name of your favorite song in the search bar, and click the download button. 2. Open the folder where the downloaded songs are stored. 3. Copy the downloaded songs to the USB flash drive, and that’s it! Of course, you need to open the U disk first, then open the U disk in "My Computer" and paste it.

How to download cad floor plans from Kujiale_How to download cad floor plans from Kujiale How to download cad floor plans from Kujiale_How to download cad floor plans from Kujiale Apr 02, 2024 pm 01:25 PM

1. Import from the pop-up window of the home-building tool when you first enter it. Click [Start Design] in Cloud Design, enter the tool page and select CAD import. Note: Currently only DWG/DXF format is supported, DWG does not exceed 5MB, and DXF does not exceed 10MB. 2. Import from the House Plan Tool. Click [Import]-[Import CAD] on the House Plan Tool page. Step 2: Adjust the imported CAD house plan. After importing into CAD, as shown in the figure below, there are some walls that have not been generated. Click the wall drawing tool in the upper left corner of the house plan interface to fill it (the red box position in the figure). After drawing the wall, on the left Select the door and window model and drag it to the wall for decoration. Note: 3D decoration design cannot be carried out for units that are not closed. After adjusting the positions of doors and windows and the door opening direction, remember to assign commands to each room.

Full analysis of Go language file renaming operation Full analysis of Go language file renaming operation Apr 08, 2024 pm 03:30 PM

The os.Rename function is used in Go language to rename files. The syntax is: funcRename(oldpath,newpathstring)error. This function renames the file specified by oldpath to the file specified by newpath. Examples include simple renaming, moving files to different directories, and ignoring error handling. The Rename function performs an atomic operation and may only update directory entries when the two files are in the same directory. Renames may fail across volumes or while a file is in use.

Implementing Machine Learning Algorithms in C++: Security Considerations and Best Practices Implementing Machine Learning Algorithms in C++: Security Considerations and Best Practices Jun 01, 2024 am 09:26 AM

When implementing machine learning algorithms in C++, security considerations are critical, including data privacy, model tampering, and input validation. Best practices include adopting secure libraries, minimizing permissions, using sandboxes, and continuous monitoring. The practical case demonstrates the use of the Botan library to encrypt and decrypt the CNN model to ensure safe training and prediction.

Security configuration and hardening of Struts 2 framework Security configuration and hardening of Struts 2 framework May 31, 2024 pm 10:53 PM

To protect your Struts2 application, you can use the following security configurations: Disable unused features Enable content type checking Validate input Enable security tokens Prevent CSRF attacks Use RBAC to restrict role-based access

PHP Microframework: Security Discussion of Slim and Phalcon PHP Microframework: Security Discussion of Slim and Phalcon Jun 04, 2024 am 09:28 AM

In the security comparison between Slim and Phalcon in PHP micro-frameworks, Phalcon has built-in security features such as CSRF and XSS protection, form validation, etc., while Slim lacks out-of-the-box security features and requires manual implementation of security measures. For security-critical applications, Phalcon offers more comprehensive protection and is the better choice.

How to enhance the security of Spring Boot framework How to enhance the security of Spring Boot framework Jun 01, 2024 am 09:29 AM

How to Enhance the Security of SpringBoot Framework It is crucial to enhance the security of SpringBoot applications to protect user data and prevent attacks. The following are several key steps to enhance SpringBoot security: 1. Enable HTTPS Use HTTPS to establish a secure connection between the server and the client to prevent information from being eavesdropped or tampered with. In SpringBoot, HTTPS can be enabled by configuring the following in application.properties: server.ssl.key-store=path/to/keystore.jksserver.ssl.k

See all articles