PHP File Download Code (supports remote file download)

Source: Internet
Author: User
Tags fread

PHP File Download Code (supports remote file download)/* This Article provides two file download codes that support local Server File Download, another one supports downloading files from remote servers to a local device.

Php tutorial File Download Code (supports remote file download)
/*
Two of the three file download codes provided in this article support downloading files from the local server, and the other supports downloading files from the remote server to the local server.
*/

Function download ($ file_dir, $ file_name)
// Parameter description:
// File_dir: directory of the file
// File_name: File Name
{
$ File_dir = chop ($ file_dir); // remove unnecessary spaces in the path
// Obtain the path of the object to be downloaded
If ($ file_dir! = '')
{
$ File_path = $ file_dir;
If (substr ($ file_dir, strlen ($ file_dir)-1, strlen ($ file_dir ))! = '/')
$ File_path. = '/';
$ File_path. = $ file_name;
}
Else
$ File_path = $ file_name;

// Determine whether the object to be downloaded exists
If (! File_exists ($ file_path ))
{
Echo 'Sorry, the file you want to download does not exist. ';
Return false;
}

$ File_size = filesize ($ file_path );
 
Header ("content-type: application/octet-stream ");
Header ("accept-ranges: bytes ");
Header ("accept-length: $ file_size ");
Header ("content-disposition: attachment; filename =". $ file_name );

$ Fp = fopen ($ file_path, "r ");
$ Buffer_size = 1024;
$ Cur_pos = 0;

While (! Feof ($ fp) & $ file_size-$ cur_pos> $ buffer_size)
{
$ Buffer = fread ($ fp, $ buffer_size );
Echo $ buffer;
$ Cur_pos + = $ buffer_size;
}

$ Buffer = fread ($ fp, $ file_size-$ cur_pos );
Echo $ buffer;
Fclose ($ fp );
Return true;

}

?>

<? Php

$ File_name = "info_check.exe ";
$ File_dir = "/public/www/download /";
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 ;}

?>


<?
// If the file path is http or ftp, the download code is as follows:

$ File_name = "info_check.exe ";
$ File_dir = "http://www.bkjia.com /";
$ File = @ fopen ($ file_dir. $ file_name, "r ");
If (! $ File ){
Echo "file not found ";
} Else {
Header ("content-type: application/octet-stream ");
Header ("content-disposition: attachment; filename =". $ file_name );
While (! Feof ($ file )){
Echo fread ($ file, 50000 );
}
Fclose ($ file );
}

?>


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.