How to download images in PHP, instead of opening-php Tutorial

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 Image Download PHP how to download images instead of opening them!
Example:
How does PHP download specified images? Instead of opening it in a browser?

PHP image download

Share:


------ 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"

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.