The file_exists function in PHP does not support Chinese name resolution _php Tips

Source: Internet
Author: User
Tags rar

In general, PHP often uses file_exists () to determine whether a file or folder exists, returns True if it exists, or returns false. However, the function does not return the correct value for the Chinese file name or folder name when the Web page uses UTF8 encoding, always returns false. After testing the solution, the analysis of the cause of this situation should be different coding caused by PHP can not be correctly judged.

The following code is code that is not able to return the correct value, regardless of whether the file is not returned:

<?php;
$file = "/attachment/21/0/Chinese. rar";
$newfile = DirName (__file__). $file;

echo file_exists ($newfile);
? >

After testing, add a sentence to convert UTF8 encoding to GB2312 encoded statements, you can correctly judge:

<?php
$file = "/attachment/21/0/Chinese. rar";
$newfile = DirName (__file__). $file;

$file =iconv (' UTF-8 ', ' GB2312 ', $file);

echo file_exists ($newfile);
? >

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.