Php implementation of simple file download method, _ PHP Tutorial

Source: Internet
Author: User
Php implements simple file download methods ,. Php implements simple file download. this example describes how php implements simple file download. Share it with you for your reference. The following describes how to download simple php files on behalf of php,

This example describes how to download simple files in php. Share it with you for your reference. The details are as follows:

The PHP file download code introduced here is a simple implementation of the download function of an image, which is not complete yet. it is best to encapsulate it into a class or use function calls. Interested friends can be improved on this basis!

The PHP file download code is as follows:

<? Php $ file_name = "2.jpg"; // define (" SPATH ","/php/image/") of the image to be downloaded /"); // The relative path for storing images $ file_sub_path = $ _ SERVER ['document _ root']; // The absolute address of the website ROOT directory $ file_path = $ file_sub_path.SPATH. $ file_name; // absolute image address, that is, the first three connections // Determine whether the object exists if (! File_exists ($ file_path) {echo "this file does not exist"; return ;}$ fp = fopen ($ file_path, "r "); // open the file $ file_size = filesize ($ file_path); // get the file size/** header used to download the file */header ("Content-type: application/octet-stream "); header (" Accept-Ranges: bytes "); header (" Accept-Length :". $ file_size); header ("Content-Disposition: attachment; filename = ". $ file_name); $ buffer = 1024; $ file_count = 0; // return data to the browser while (! Feof ($ fp) & $ file_count <$ file_size) {$ file_con = fread ($ fp, $ buffer); $ file_count + = $ buffer; echo $ file_con; // if no echo exists, only files of 0 bytes will be downloaded.} fclose ($ fp);?>

I hope this article will help you with php programming.

Example: This article describes how to download a simple php file. Share it with you for your reference. The details are as follows: PHP file download generation described here...

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.