File download problems-some word files are garbled after being downloaded or a warning is displayed. the code is as follows. Please advise. -Php Tutorial

Source: Internet
Author: User
File download problems-some word files are garbled or warning is displayed after being downloaded. the code is as follows. Please advise... $ Download]; get the ID & nbsp; $ connmysql_connect (localhost, root,) in the file database from the current page; * if (! $ Conn) & nbsp; {echo (& lt; B & gt; database connection file download problem-garbled characters or warnings are displayed after some word files are downloaded. the code is as follows. Please advise...
$ Id = $ _ GET ['download']; // obtain the ID of the file database from the current page.
$ Conn = mysql_connect ('localhost', 'root ','');
/* If (! $ Conn)
{
Echo (' Database Connection failed!');
Exit;
}
Else echo' Connection successful!';*/
$ Flag = mysql_select_db ('laborder', $ conn );
$ SQL = "select * from files where files_id = $ id ";
Mysql_query ("set names gbk ");
$ Result = mysql_query ($ SQL, $ conn );
$ Lists = mysql_fetch_array ($ result );
$ File_name = $ lists ["files_name"]; // get the file name
$ File_dir = "uploadfiles/"; // Mark the directory where the file is located

$ File = fopen ($ file_dir. $ file_name, "r"); // open the file
// Input file tag
Header ("Content-type: application/octet-stream ");
Header ("Accept-Ranges: bytes ");
Header ("Accept-Length:". filesize ($ file_dir. $ file_name ));
Header ("Content-Disposition: attachment; filename =". $ file_name );
Echo fread ($ file, filesize ($ file_dir. $ file_name ));
Fclose ($ file );
Exit;

------ Solution --------------------
PHP code
$ Contents = fread ($ file, filesize ($ file_dir. $ file_name); echo iconv ("UTF-8", "GBK", $ contents); fclose ($ file); exit;
------ Solution --------------------
Discussion

No worries!

Save the file as UTF8 without BOM

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.