Php code implementation of the file download function _ PHP Tutorial

Source: Internet
Author: User
Tags php download
Php code sharing for file download. A simple php file downloads the source code. although resumable data transfer is not supported, it can meet some common requirements. Php files can be downloaded using the tag. for example, if a simple php file named ahr downloads the source code, resumable upload is not supported, but it can meet some common requirements. Php download file is actually implemented with a tag, such as magento-1.8.1.0.zip. However, when a page view can identify the format, such as. txt,. html,. txt, and so on, then use abc.txt to know what will happen.

The code is as follows:


/**
* File download
*
**/

Header ("Content-type: text/html; charset = utf-8 ");
Download ('web/magento-1.8.1.0.zip ', 'magento download ');

Function download ($ file, $ down_name ){
$ Suffix = substr ($ file, strrpos ($ file, '.'); // get the file suffix
$ Down_name = $ down_name. $ suffix; // The new file name is the downloaded name.

// Determine whether a specified file exists
If (! File_exists ($ file )){
Die ("the file you want to download does not exist, it may be deleted ");
}
$ Fp = fopen ($ file, "r ");
$ File_size = filesize ($ file );
// The header used to download the object
Header ("Content-type: application/octet-stream ");
Header ("Accept-Ranges: bytes ");
Header ("Accept-Length:". $ file_size );
Header ("Content-Disposition: attachment; filename =". $ down_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;
}
Fclose ($ fp );
}

?>


You can also look at the detailed code of this annotation:

The code is as follows:


// Download an image
// $ File_name = "angelove ";

$ File_name = "bjnihao.jpg"; // when a Chinese program cannot be downloaded, the following error occurs: the file does not exist.
// Transcode the file (the php file function is old and must convert the Chinese code to gb2312)
// Iconv-Convert string to requested character encoding
// By www.jb51.net
$ File_name = iconv ("UTF-8", "gb2312", $ file_name );

// Set the file download path (relative path)
// $ File_path = "./dowm/". $ file_name;

// Use absolute path
$ File_path = $ _ SERVER ['document _ root']. "/http/dowm/". $ file_name;

// Open the file --- first judge and then operate
If (! File_exists ($ file_path )){

Echo "the file does not exist ";
Return; // exit directly
}

// Exists -- open the file

$ Fp = fopen ($ file_path, "r ");

// Get the file size
$ File_size = filesize ($ file_path );

// Response header required for http download
Header ("Content-type: application/octet-stream"); // The Returned file
Header ("Accept-Ranges: bytes"); // returns the value in bytes.
Header ("Accept-Length: $ file_size"); // returns the file size.
Header ("Content-Disposition: attachment; filename =". $ file_name); // The pop-up dialog box on the client, with the corresponding file name

// Return data to the client
// Set the output size
$ Buffer = 1024;

// To ensure secure download, we 'd better make a file byte read counter
$ File_count = 0;
// Determine whether the file pointer is at the end of the file (whether the file is read)
While (! Feof ($ fp) & ($ file_size-$ file_count)> 0 ){

$ File_data = fread ($ fp, $ buffer );
// Count the number of bytes read
$ File_count + = $ buffer;
// Return some data to the browser
Echo $ file_data;
}
// Close the file

Fclose ($ fp );
?>

Encapsulation function:

/*
Encapsulation function:
Parameter description ---- $ file_name: file name
$ File_sub_dir: the sub-path for file download
*/
Function file_dowm ($ file_name, $ file_sub_dir ){
// File transcoding
$ File_name = iconv ("UTF-8", "gb2312", $ file_name );

// Use absolute path
$ File_path = $ _ SERVER ['document _ root']. "$ file_sub_dir". $ file_name;

// Open the file --- first judge and then operate
If (! File_exists ($ file_path )){

Echo "the file does not exist ";
Return; // exit directly
}

// Exists -- open the file

$ Fp = fopen ($ file_path, "r ");

// Get the file size
$ File_size = filesize ($ file_path );
/*
// You can set the maximum number of objects to be downloaded.
If ($ file_size> 50 ){
Echo "the file is too large to be downloaded ";
Return;
}*/

// Response header required for http download
Header ("Content-type: application/octet-stream"); // The Returned file
Header ("Accept-Ranges: bytes"); // returns the value in bytes.
Header ("Accept-Length: $ file_size"); // returns the file size.
Header ("Content-Disposition: attachment; filename =". $ file_name); // The pop-up dialog box on the client, with the corresponding file name

// Return data to the client
// Set the output size
$ Buffer = 1024;

// To ensure secure download, we 'd better make a file byte read counter
$ File_count = 0;
// Determine whether the file pointer is at the end of the file (whether the file is read)
While (! Feof ($ fp) & ($ file_size-$ file_count)> 0 ){

$ File_data = fread ($ fp, $ buffer );
// Count the number of bytes read
$ File_count + = $ buffer;
// Return some data to the browser
Echo $ file_data;
}

// Close the file
Fclose ($ fp );
}

File_dowm ("bjnihao.jpg", "/http/dowm /");
?>

Another Code:

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 );
}
}

Bytes. Php can actually download files with a tag a, such as a hr...

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.