How to download images in PHP instead of opening

Source: Internet
Author: User
Tags php download
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 /");

?>

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.