Php file download solution

Source: Internet
Author: User
PHP file downloading: the solution to corrupted PHP file content with garbled characters requires PHP file downloading.

Just copy the code and change it.

A lot of results have also come out.

After clicking download, I thought the file was downloaded. who knows that the file was damaged when the image was opened, so I observed the file size. it seems that it is about 1 KB more.

Use PS to open it. well, the system prompts that the image is damaged, but the image can be displayed.

So I think it's the extra 1kb problem.

Then, I upload a txt file and download it.

It was found that the front and back of the file contain a few more characters.

Then I was wondering how to remove it. various Baidu, Google, and questions on the forum.

Finally, after more than an hour of searching, 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);    }}

Note:

ob_clean();flush();

This is the key. with these two functions, the image is opened normally and 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.