How does PHP download images instead of opening them! Example: PHP & nbsp; how can I download a specified image? Instead of opening it in a browser? PHP how to download images instead of opening them!
Example:
How does PHP download specified images? Instead of opening it in a browser? Download and share PHP images:
------ Solution --------------------
$file = "/tmp/dummy.tar.gz";
header("Content-type: application/octet-stream");
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
header("Content-Length: ". filesize($file));
readfile($file);
------ Solution --------------------
I will post you a story about how Han Shunping talked about php download, hoping to help you. If you need to watch the video, you can watch Han Shunping's php video tutorial 87th:
The code is as follows:
// Description of the function
// Parameter description $ file_name file name
// $ File_sub_dir: the sub-path of the downloaded file '"/xxx /"
Function down_file ($ file_name, $ file_sub_dir ){
// Click it to download an image.
// If the file is in Chinese.
// The cause is the php file function, which is relatively old and needs to be transcoded for Chinese gb2312.
$ File_name = iconv ("UTF-8", "gb2312", $ file_name );
// Absolute path
$ File_path = $ _ SERVER ['document _ root'] [
]. $ File_sub_dir. $ file_name;
// 1. open the file
If (! File_exists ($ file_path )){
Echo "the file does not exist! ";
Return;
}
$ Fp = fopen ($ file_path, "r ");
// 2. process files
// Obtain the size of the downloaded object
$ File_size = filesize ($ file_path );
If ($ file_size> 30 ){
Echo" Window. alert ('oversized ') script ";
Return;
}
// Returned file
Header ("Content-type: application/octet-stream ");
// Returns the value in bytes.
Header ("Accept-Ranges: bytes ");
// Returns the file size.
Header ("Accept-Length: $ file_size ");
// The pop-up dialog box on the client, with the corresponding file name
Header ("Content-Disposition: attachment; filename =". $ file_name );
// Send data back to the client
$ Buffer = 1024;
// To ensure secure download, we 'd better make a file byte read counter
$ File_count = 0;
// This statement is used to determine whether the object is ended.
While (! Feof ($ fp) & ($ file_size-$ file_count> 0 )){
$ File_data = fread ($ fp, $ buffer );
// Count the number of bytes read
$ File_count + = $ buffer;
// Send some data back to the browser;
Echo $ file_data;
}
// Close the file
Fclose ($ fp );
}
// Test whether the function is available
Down_file ("shun Ping .jpg", "/http/down /");
?>