PHP Remote Download picture corruption problem

Source: Internet
Author: User

The code is as follows:

<?php header ("Content-type=html/text;charset=utf-8"); function Download ($file _name, $file _sub_dir) {$file _name = iconv ("Utf-8", "gb2312", $file _name); $file _path = $_server[' Document_root ']. $file _sub_dir. $file _name;if (!file_exists ($file _path)) {echo "file does not exist"; return;} $fp = fopen ($file _path, "RB"), $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); $buffer = 1024x768; $file _count = 0;while (!feof ($fp) && $file _size-$file _count>0) {$file _data = Fread ($fp, $buffer) $file _count+= $buffer; echo $file _data;} Fclose ($FP);} Download ("Picture1.jpg", "/txt/");?>

The downloaded picture is successful, but when the picture is opened, the display picture is corrupted;

I spent one hours debugging, and finally found the problem: The reason for the file encoding;

The new file must be Utf-8 (Special note: Save with Notepad as Utf-8, you must create a new Utf-8 file), and use an editor such as Notepad++,sublime.

PHP Remote Download picture corruption problem

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.