Download png format pictures, pop out php file format download, seek to solve

Source: Internet
Author: User
Tags fread
function Down_file ($file _name, $file _path) {
@ $file _name=iconv ("Utf_8", "gb2312", $file _name);
$file _path=$_server[' Document_root '). $file _path. $file _name;
if (!file_exists ($file _path)) {
echo "file does not exist";
Return
}
$FP =fopen ($file _path, "r+");
$file _size=filesize ($file _path);
Header ("Content-type:application/octet-stream");
Header ("Accept-rangers:bytes");
Header ("Accept-length: $file _size");
Header ("Content-dispostion:attachment;filename=". $file _name);
while (!feof ($file _path)) {
$data =fread ($FP, 1024);
Echo $data;
}
Fclose ($FP);
}

Down_file ("Php.png", "/down/");
?>


Reply to discussion (solution)

Add a download type to try the header ("Content-type:image/png");

Same #

Header ("Content-dispostion:attachment;filename=". $file _name);
This is a path, isn't it?
Header ("Content-dispostion:attachment;filename=". BaseName ($file _name));
That's it.




Thank you, three people, you're right.

1? and 2?? Bit ...
Add header ("Content-type:image/png");? useful, because it is the next, not the.
First of all, you don't have code at all. Yes,???? The

@ $file _name=iconv ("Utf_8", "gb2312", $file _name);?? It's UTF-8.
while (!feof ($file _path)) {?? It's $fp.
Header ("Content-dispostion:attachment;filename=". $file _name);?? It's disposition.

Change to?? You can do it.

function Down_file ($file _name, $file _path) {@ $file _name=iconv ("Utf-8", "gb2312", $file _name); $file _path=$_server[' Document_root ']. $file _path. $file _name;if (!file_exists ($file _path)) {echo "file does not exist"; return;} $FP =fopen ($file _path, "r+"), $file _size=filesize ($file _path); Header ("Content-type:application/octet-stream"); Header ("Accept-rangers:bytes"), Header ("Accept-length: $file _size"), Header ("content-disposition:attachment; Filename= ". $file _name), while (!feof ($fp)) {$data =fread ($fp, 1024x768); echo $data;}   Fclose ($FP);} Down_file ("Php.png", "/down/");
  • Related Article

    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.