PHP Download related issues, the contents of the download file is empty, the picture is not previewed

Source: Internet
Author: User
Tags php download ranges
PHP download problem, after downloading the contents of the file is empty, the picture is not previewed
There are two files, a.php and b.php
Click the link in a.php to start downloading the file: The code is as follows

This is a.php.

$file _dir = "D:/files";
$file _name = $row [1]; This is from the database.
echo "". $row [2]. ""; Click this link, download the file name is right, the file is not content, if it is a picture, the picture is not previewed
?>


This is all the code for b.php.

$file _name = $_get[' file_name '); Filename
$file _dir = $_get[' File_dir '); File path

if (!file_exists ($file _dir. $file _name)) {
echo "Could not find [". $file _dir. $file _name. "] Document";
Exit
}else{
$file =fopen ($file _dir. $file _name, "R");

Header ("Content-type:application/octet-stream");
Header ("Accept-ranges:bytes");
Header ("Accept-length:". FileSize ($file _dir. $file _name));
Header ("content-disposition:attachment; Filename= ". $file _name);
}
?>

Share to:


------Solution--------------------
a.php is $file _dir = "D:/files";
Then in the b.php of the $file _dir. $file _name is missing a path character

You have only $file =fopen in b.php ($file _dir $file _name, "R"); Opened the file, but did not output the contents of the file

If using $file =fopen ($file _dir. $file _name, "R"); The open file is a picture, you will not be able to read the picture data correctly
Should be opened in binary mode: $file =fopen ($file _dir. $file _name, "R b");
------Solution--------------------
This post was last edited by xuzuning on 2013-06-08 16:08:01

    }else{
$file = $file _dir. $file _name;
Header ("Content-type:application/octet-stream");
Header ("Accept-ranges:bytes");
Header ("Accept-length:". FileSize ($file));
Header ("content-disposition:attachment; Filename= ". $file _name);
ReadFile ($file);
}
  • 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.