PHP implementation browser file download picture or PHP file

Source: Internet
Author: User
Tags php file php code readfile

Hyperlink download

If the browser supports hyperlinks to download the file, it will automatically download the file when you click the hyperlink.

Here's a test to download the file via the hyperlink:

<a href= "/uploads/test.zip" > Download files via hyperlinks </a><br>

When we click on it, it looks like this:

Hyperlink download File

To download a picture from a hyperlink:
When we download a picture through a hyperlink, it opens the display directly in the browser, and then we need to save it to the specified path:


<a href= "/uploads/qc8054r7qpgq_1000x500.jpg" > Download pictures via Hyperlinks </a><br>

The effect is this:

Hyperlink Download Picture

Download by program

HTML-side line of code

<a href= "download.php?filename=qc8054r7qpgq_1000x500.jpg" > Download picture </a> by program Way

download.php Code

We send the file download information through header request headers, specify to download the attachment, download the filename, content-length to specify the size of the file, and then use the ReadFile function to read the contents of the file and implement the file download:

<?php

$filename = $_get[' filename '];
Header (' Content-disposition:attachment;filename= '. basename ($filename));
Header (' Content-length: ' FileSize ($filename));

ReadFile ($filename);

The effect is as follows:

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.