In PHP, fopen cannot create a Chinese file name.

Source: Internet
Author: User

Previously, chartset on the Web page used UTF-8, and the file also used UTF-8. Then, when fopen () was used to create a file with a Chinese file name, the problem was solved. The file names were all messy.
Code!

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
Baidu confirmed this, so my webpage should 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
And changed the include webpage 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 only the file name parameter in fopen ().
Number of separate codes for it, PHP has iconv ()
This encoding change program converts UTF-8
Gb2312 can 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">
<HTML>
<Head>
<Meta content = "text/html; charsets = UTF-8" http-equiv = "Content-Type">
<Title> title: {Title} </title>
</Head>
<Body>
<B> content of the news: </B> {content}
</Body>
</Html>

 

 

 

Test. php

 

<? PHP

// The actual application may be to query the database to retrieve the content.
$ Rows = array (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 Chinese file names
$ 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! ";
?>

 

 

From: http://blog.csdn.net/lovewqww/archive/2008/03/20/2200949.aspx

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.