php file download appears download file content garbled corrupt solution

Source: Internet
Author: User
In the PHP program, you need to use PHP to do the file download

Just do it. Copy the code and change it.

There's a lot of effects coming out.

Then click on the download, um, I thought the file download, who knows, the picture opened when the prompt file corruption, which I observed the size of the file. It seems to be a lot of about 1KB.

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

Then I think it's the more 1kb problem.

Then, I upload a txt and then download it.

Sure enough, there were a few more characters in front and behind the file.

Then I was wondering how to get rid of it. Various 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. With these two functions, done, the picture is open normally, the text is not garbled.


Reprinted from: Http://www.9958.pw/post/download_content

  • 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.