Php download problem. after the download, the file content is blank and the image is not previewed.

Source: Internet
Author: User
Tags php download ranges
Php download problem. after the download, the file content is blank, and the image is not previewed. There are two files, a. php and B. php.
Click the link in a. php to download the file: the code is as follows:

This is a. php
 


This is all B. php code.
 


Reply to discussion (solution)

In a. php, $ file_dir = "D:/files ";
In B. php, $ file_dir. $ file_name lacks the path character.

In B. php, you only have $ file = fopen ($ file_dir. $ file_name, "r"); opened the file, but no output file content.

If $ file = fopen ($ file_dir. $ file_name, "r") is used; if the opened file is an image, the image data cannot be read correctly.
Open in binary mode: $ file = fopen ($ file_dir. $ file_name, "r B ");

In a. php, $ file_dir = "D:/files ";
In B. php, $ file_dir. $ file_name lacks the path character.

In B. php, you only have $ file = fopen ($ file_dir. $ file_name, "r"); opened the file, but no output file content.

If $ file = fopen ($ file_dir. $ file_name, "r") is used; if the opened file is an image, the image data cannot be read correctly.
Open in binary mode: $ file = fopen ($ file_dir. $ file_name, "r B ");

I understand it, but I changed it to this. The result code is as follows:

 

The font color cannot be changed in the dizzy code.

 

This post was last edited by xuzuning at 16:08:01 on

    }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);    }

Thanks to the moderator. the moderator speaks very well and must share the following information:
It is used to download compressed files, including the files in the compressed files, and the code is not difficult

// Open the file $ file = fopen ($ file_dir. $ file_name, "r"); // The Returned file type Header ("Content-type: application/octet-stream "); // return the Header ("Accept-Ranges: bytes") according to the byte size; // return the file size Header ("Accept-Length :". filesize ($ file_dir. $ file_name); // The pop-up dialog box for the client, the corresponding file name Header ("Content-Disposition: attachment; filename = ". $ file_dir. $ file_name); // transmit data to the client echo fread ($ file, filesize ($ file_dir. $ file_name); // after modification, only 1024 bytes of data are transmitted at a time to the client. // Send data back to the client $ buffer = 1024; // Determine whether the file has been read while (! Feof ($ file) {// read the file into the Memory $ file_data = fread ($ file, $ buffer ); // send 1024 bytes of data back to the client each time echo $ file_data;} fclose ($ file );

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.