Talking about the problem that fopen cannot create a Chinese file name file in php,

Source: Internet
Author: User

Talking about the problem that fopen cannot create a Chinese file name file in php,

Previously, the chartset on the Web page used UTF-8 and the file also used UTF-8. Then, the problem arises when you use fopen () to create a file with a Chinese file name. The file names are all garbled!

I checked a lot of documents and tried a lot of methods to solve the problem. I was thinking about using other methods to bypass this problem. Suddenly, my mind flashed the default Windows text encoding is ansi, and then I ran baidu again, I confirmed this, so my webpage should also be ansi encoded so that the created file name will not be garbled.

Then I started to verify that the web page was saved in ansi, And the chartset statement was removed. It was OK, but the content of the Web page became garbled. Later I remembered that this web page also included other web pages, change the include web page to ansi for saving. Haha everything is OK.

Programming really depends on accumulation. If I have never seen that the default Windows code is ansi, then I don't know how many months can this problem be solved.

Ps:<Meta content = "text/html; charset = UTF-8" http-equiv = "Content-type"> this meta tag must be placed before <title> </title>.

Later, I came up with a better solution. The webpage still uses UTF-8 encoding and storage, but the parameter numbers of file names in fopen () can be coded separately, php has the iconv () code change program. Convert UTF-8 to gb2312 to avoid garbled Chinese file names.

Test.htm

<! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd"> 

Test. php

<? Php // in actual applications, it is likely to query the database to retrieve the content. $ Rows = array ("Replace title 1", "replace content 1"), array ("Replace title 2", "replace content 2 ")); $ filename = "tmp.htm"; foreach ($ rows as $ id => $ val) {$ title = $ val [0]; $ content = $ val [1]; $ pagename = "test ". $ id. ". html "; // encode the file name to avoid garbled characters in the Chinese file name $ pagename = iconv (" UTF-8 "," GBK ", $ pagename ); // read the 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 a new file $ newpage = fopen ($ pagename, "w "); fwrite ($ newpage, $ string); fclose ($ newpage);} echo "created successfully! ";?>

In the above discussion, the problem that fopen cannot create a Chinese file name file in php is all the content that I shared with you. I hope you can give us a reference and support the help house a lot.

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.