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 the beginning, the Chinese file name displayed in the file list on the page is garbled, and mysql_query ("SET & nbsp; NAMES & nbsp; 'utf8'") is added. The PHP file is being downloaded with garbled characters.
I want to add a file upload/download function. because the file size may be large, the idea is to store the file in the database in the server path. when downloading the file, read the file according to the path. 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.

 Function DownloadFile ($ mineid, $ notice = null ){
Global $ smarty;
NeedLogin ();
// Session_cache_limiter ('private ');
$ Session = Session: start ();
$ CurUser = $ session-> curUser;
$ Logger = new CategoryLogger ('log _ definition ');
If ($ notice! = "Auto") notice ($ notice );

$ Id = $ mineid; // ID of the object to be downloaded
If (! Isset ($ id) or $ id = "") die ("error: id none"); // Location record, 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 file exists
Echo "file not found ";
Exit;
} Else {
$ FilePath = $ file_dir; // The location of the file you downloaded on the server is shown here.
$ FileName = $ file_name;
// The downloaded file name is provided here
$ File = fopen ($ filePath, "r"); // 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;
}
}
?>

------ 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, "rb ");

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

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.