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

PHP实现文件安全下载的实例_PHP

Jun 01, 2016 pm 12:31 PM
file download Safety Example accomplish document

程序如下:

  $file_name = "info_check.exe";

  $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 = "info_check.exe";

  $file_dir = "www.easycn.net/";

  $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 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)

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 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.

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.

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.

How should the Java framework security architecture design be balanced with business needs? How should the Java framework security architecture design be balanced with business needs? Jun 04, 2024 pm 02:53 PM

Java framework design enables security by balancing security needs with business needs: identifying key business needs and prioritizing relevant security requirements. Develop flexible security strategies, respond to threats in layers, and make regular adjustments. Consider architectural flexibility, support business evolution, and abstract security functions. Prioritize efficiency and availability, optimize security measures, and improve visibility.

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.

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

See all articles