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.