Php file download garbled

Source: Internet
Author: User
I want to add a file upload/download function for PHP file download garbled. because the file may be large, the idea is to store the file on the server path in the database and read the file according to the path during the download. At first, the Chinese file name displayed in the file list on the page is garbled and added
mysql_query("SET NAMES 'utf8'");
Then solve the problem. Later, the file name stored during download is garbled, and this code is also added to solve the problem. However, the Chinese characters in the downloaded file are garbled. I use txt text test, English no problem, UTF-8 format of the text is no problem, the default ANSI will become garbled, of course, the upload word file is also garbled. Everyone to help look at how to change, do not know is to pass to the server is garbled, or download the format is not converted, my database is UTF-8 encoding, master page is also UTF-8 encoding.

 CurUser; $ logger = new CategoryLogger ('log _ definition '); if ($ notice! = "Auto") notice ($ notice); $ id = $ mineid; // The ID of the object to be downloaded if (! Isset ($ id) or $ id = "") die ("error: id none"); // locate the record and read $ conn = mysql_connect ('10. 4.16.102 ', 'root', 'ABC') or die ('unable to connect. '); mysql_select_db ('greenmine', $ conn) or die (mysql_error ($ conn); mysql_query ("set names 'utf8 '"); $ SQL = "select * from file where id = $ id"; $ result = mysql_query ($ SQL); if (! $ Result) die ("error: mysql query"); $ num = mysql_num_rows ($ result); if ($ num <1) die ("error: no this recorder "); $ data = mysql_result ($ result, 0, "file_data"); // file storage path $ type = mysql_result ($ result, 0, "file_type "); $ name = mysql_result ($ result, 0, "file_name"); mysql_close ($ conn); $ file_name = $ name; $ file_dir = $ data; if (! File_exists ($ file_dir) {// check whether the echo file exists. "the file cannot be found"; exit;} else {$ filePath = $ file_dir; // Here is the location of the downloaded file on the server $ fileName = $ file_name; // the downloaded file name $ file = fopen ($ filePath, "r") is provided here "); // open the file Header ("Content-type: application/octet-stream"); Header ("Accept-Ranges: bytes"); Header ("Accept-Length :". filesize ($ filePath); Header ("Content-Disposition: attachment; filename = ". $ fileName); // output file content echo fread ($ File, filesize ($ filePath); fclose ($ file); exit ;}}?>


Reply to discussion (solution)

Is the file name garbled or the file content garbled?
Only file names are saved in your library, while files are saved independently. Isn't that true?
To open a file, you need $ file = fopen ($ filePath, "r B ");

File name for download
Header ("Content-Disposition: attachment; filename =". $ fileName );
Supporting the operating system used by the user

Is the file name garbled or the file content garbled?
Only file names are saved in your library, while files are saved independently. Isn't that true?
To open a file, you need $ file = fopen ($ filePath, "rb ");

File name for download
Header ("Content-Disposition: attachment; filename =". $ fileName );
Supporting the operating system used by the user
...... The file name is correct, and the content is garbled. Files are stored separately on the server. It is not easy to change to rb.

As long as you have not performed operations on the file content, you can only say that the file itself is garbled

As long as you have not performed operations on the file content, you can only say that the file itself is garbled! Solved the problem, the php page encoding format UTF-8 + bom into UTF-8 on the line, also thank you!

May I ask the landlord to upload a word file such as doc or docx? will the content of the file be properly displayed after being downloaded?

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.