Simple Example of php implementation file download (code implementation file download)

Source: Internet
Author: User
This article mainly introduces a simple example of php file downloading. For more information, see

This article mainly introduces a simple example of php file downloading. For more information, see

The Code is as follows:


Public function downloads ($ name ){
$ Name_tmp = explode ("_", $ name );
$ Type = $ name_tmp [0];
$ File_time = explode (".", $ name_tmp [3]);
$ File_time = $ file_time [0];
$ File_date = date ("Y/md", $ file_time );
$ File_dir = SITE_PATH. "/data/uploads/$ type/$ file_date /";

If (! File_exists ($ file_dir. $ name )){
Header ("Content-type: text/html; charset = UTF-8 ");
Echo "File not found! ";
Exit;
} Else {
$ File = fopen ($ file_dir. $ name, "r ");
Header ("Content-type: application/octet-stream ");
Header ("Accept-Ranges: bytes ");
Header ("Accept-Length:". filesize ($ file_dir. $ name ));
Header ("Content-Disposition: attachment; filename =". $ name );
Echo fread ($ file, filesize ($ file_dir. $ name ));
Fclose ($ file );
}
}


Example 2: Download the code

Generally, URLs are called to download files. However, this method cannot be used to identify files opened by ie, such as downloading an image or html webpage, in this case, programming is required. The following php code can be used:

The Code is as follows:


If (empty ($ _ GET ['filename']) | empty ($ _ GET ['filedir']) | empty ($ _ GET ['fileid']) {
Echo 'script alert ("illegal connection! "); Location. replace (" index. php ") script '; exit ();
}
$ File_name = $ _ GET ['filename'];
$ File_dir = $ _ GET ['filedir'];
$ FileId = $ _ GET ['fileid'];
$ File_dir = $ file_dir ."/";
If (! File_exists ($ file_dir. $ file_name) {// check whether the file exists
Echo "file not found ";
Exit;
} Else {
$ File = fopen ($ file_dir. $ file_name, "r"); // open the file
// Input file tag
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 );
// Output file content
Echo fread ($ file, filesize ($ file_dir. $ file_name ));
Fclose ($ file );
Exit ();
}
?>

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.