PHP File Download garbled problem

Source: Internet
Author: User
I want to add a file upload download function, because the file may be large, so the idea is to store the file in the database path on the server, download the file according to the path read. At the beginning of the upload page file list displays the Chinese file name is garbled, added a
mysql_query ("SET NAMES ' UTF8 '");
Resolved afterwards. Later in the download when the file name is garbled, add the same code to solve. But the download of the file inside the Chinese is garbled. I use TXT text test, English no problem, UTF-8 format text also no problem, the default ANSI is garbled, of course, upload Word file is garbled. Everyone help to see how to change, do not know is to upload to the server is garbled, or download down format is not converted, my database is UTF-8 encoded, master page is also UTF-8 encoding.

 Curuser; $logger = new Categorylogger (' log_definition '), if ($notice! = "Auto") notice ($notice); $id = $mineid;//The ID of the file to download  if (!isset ($id) or $id = = "") Die ("Error:id none");       Locate records, 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 file for presence of echo "File not found";      Exit       } else {$filePath = $file _dir;//here is where you download the file on the server $fileName = $file _name; Here is the filename you downloaded $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 contents echo fread ($file, FileSize ($filePath));       Fclose ($file);     Exit }}?>


Reply to discussion (solution)

is the filename garbled, or the file content garbled?
Only the file names are saved in your library, and the files are saved independently. Isn't that right?
Open file to $file = fopen ($filePath, "R B");

File name for download
Header ("content-disposition:attachment; Filename= ". $fileName);
To match the operating system used by the user

is the filename garbled, or the file content garbled?
Only the file names are saved in your library, and the files are saved independently. Isn't that right?
Open file to $file = fopen ($filePath, "RB");

File name for download
Header ("content-disposition:attachment; Filename= ". $fileName);
To match the operating system used by the user
...... The file name is right and the contents are garbled. The files are saved separately on the server. It's not good to change to RB.

As long as you do not manipulate the contents of the file, then you can only say that the file itself is garbled

As long as you do not work on the contents of the file, then can only say that the file itself is garbled Ah! Problem solved, the PHP page encoding format Utf-8+bom changed to UTF-8 on the line, the same thanks to the owner!

Ask the landlord, upload doc or docx this word file, downloaded after the open file content is normal display?

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