php file download appears download file content garbled corrupted solution (recommended) _php instance

Source: Internet
Author: User
Tags flush

When doing PHP programs, you need to use PHP to do the file download

Then do it. Copy the code and change it.

A lot of the effects are coming out.

Then click to download, well, I thought that the file download, who knows, the picture opened when prompted file damage, which I observed the size of the file. It seems to be about 1KB more.

Use PS to open, well, prompted the damage, but the image can come out.

So I think it's the 1kb problem.

Then, I put a txt upload up, and then download.

It turns out that there are a few more characters in front and back of the file.

Then I'm thinking about how to get rid of it. All kinds of Baidu, Google, on the forum to ask questions.

Finally, after my one-hour search, I finally found a solution.

The code is as follows

Public function Download ()
{
  $upload =m (' upload ');
  $data = $upload->find ($_get[' id '));

  if (empty ($data)) {
    header (' http/1.0 404 Not Found ');
    Header (' Location:. ');
  } else{

  $path = './public/uploads/'. $data [' Savename '];

  Header ("Content-type:". $data [' Type ']);

  Header (' Content-disposition:attachment filename= "'. $data [' name ']. '"];

  Header (' Content-length: '. $data [' size ']);

  Ob_clean ();
  Flush ();

  ReadFile ($path);

  }

Attention:

Ob_clean ();
Flush (); This is the key. Use these two functions, get it, the picture is open normally, the text is not garbled.

The above php file download appears download file content garbled corrupted solution (recommended) is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.