PHP File Download Instance code detailed

Source: Internet
Author: User
This article mainly for you in detail the php file download instance code, with a certain reference value, interested in small partners can refer to

The function of file download is basically necessary for a website, so let's take a look at how PHP implements the file download today.

No control type

The type of uncontrolled type referred to here is those types of resources that can be downloaded directly without adding PHP code control. Generally, compressed files. rar; Audio and video files:. Avi and so on can also be downloaded directly. However, the audio and MP4 will be directly parsed by the browser.
Such as:

. avi file

. rar Files

. Mp4,mp3, pictures, etc. will be parsed directly

Core code

Using PHP code to control the download of the file is very simple. We live with very little code to do such a complex job.

Type

Use the basename function to get the name of the file instead of the path information, protecting the server's directory security header ("Content-disposition:attachment;filename=". BaseName ($ filename));

Length

Header ("Content-length:". FileSize ($filename));

Implementation functions

<?php$filename = $_get[' filename '];//uses the basename function to obtain the name of the file instead of the path information, protecting the server's directory security header ("Content-disposition: Attachment;filename= "$filename); Header (" Content-length: ". FileSize ($filename)); ReadFile ($filename); >

Optimization

We didn't use a function called basename when we specified the file name, so that when we downloaded the file, we would have the full path to the file on the server.

Note: The name of the downloaded file must not contain its full path name on the server, or it may cause a partial resource leak. Even though there are ways to control access to resources, it is good to use the basename function.

The name of the original download file

Optimized file Download name

Summarize

PHP file download is really too convenient to use compared to Java implementation. Within the download file, there is a lot of information provided to us, through these packaged information, we can achieve a more elegant file download system.

Summary: The above is the entire content of this article, I hope to be able to help you learn.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.