Fopen cannot create Chinese file name files in PHP

Source: Internet
Author: User
This article mainly introduces PHP fopen can not create the Chinese file name files, interested in the reference of friends, I hope to be helpful to everyone.

The previous page Chartset with the Utf-8, the file is also used Utf-8, and then use fopen () to create a file with a Chinese filename when the problem comes out, filenames are garbled!

See a lot of documents tried a lot of methods can not solve, originally thought to use other methods to circumvent this problem, suddenly the mind flashed Windows default text encoding is ANSI, and then Baidu a bit, confirmed this, so my page should also be ANSI encoding to make the file name is not garbled.

Then proceed to verify, the Web pages are used ANSI save, remove the Chartset statement, sure enough, but the content of the page is garbled, and later thought, this page also includes other pages, the Include page also changed to ANSI save, haha everything OK

Programming this job really depends on the accumulation, if I have not seen the Windows default encoding is ANSI, then this problem does not know what year and months to solve

PS:< meta content = "text/html; Charset=utf-8 "http-equiv =" Content-type "> this meta tag must be placed before <title></title> is valid

Later thought of a better solution, the Web page or utf-8 encoding and saving, just fopen () in the file name parameter alone to it code on the line, PHP has iconv () This change Code program, the Utf-8 to gb2312 can avoid the Chinese file name is garbled

Test.htm

<! DOCTYPE Html>

test.php

<?php  //Actual application is likely to query the database to fetch content.  $rows = Array ("Replace header 1", "Replace content 1"), Array ("Replace Heading 2", "Replace content 2");  $filename = "tmp.htm";  foreach ($rows as $id = = $val) {    $title = $val [0];    $content = $val [1];    $pagename = "Test". $id. ". HTML ";     The encoding of the file name, avoid garbled Chinese file name    $pagename = Iconv ("UTF-8", "GBK", $pagename);         Read template    $tmpfile = fopen ($filename, "R");    $string = Fread ($tmpfile, FileSize ($filename));    $string = Str_replace ("{title}", $title, $string);    $string = Str_replace ("{content}", $content, $string);    Fclose ($tmpfile);    Write new file    $newpage = fopen ($pagename, "w");    Fwrite ($newpage, $string);    Fclose ($newpage);      }  echo "created successfully! ";? >

The above is the whole content of this article, I hope that everyone's study has helped.


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.