Make file paths must be English

I am trying to check a directory with PHP:

is_dir('C:\Users\\Desktop\ ') 

But the result is always false. Should I correctly indicate the directory in English for PHP?

+4
source share
6 answers

try using utf-8 in script also check slashes

+4
source

In the windows

The file system is always UCS-2, unfortunately PHP is not that smart. I'm not sure if is_dir() comes down to calling an ANSI or WideString API, but it makes sense to go with ANSI. In this case, you are at the mercy of installing the Language for Non-Unicode OS. File names in the wrong languages ​​will not be available to you.

On linux

It's not so easy. The file system itself does not really have a specific text encoding, which makes things uncomfortable. The Cyrillic file name can be saved in UTF-8 or Windows-1252 (or something else), and it refers to software that creates / reads files to find out what the encoding is. The file system simply stores a bunch of bytes as a "file name". PHP also doesn't care about text encodings, so you really need to know what the encoding of the file name is in advance, so you can pass the correct string to is_dir() .

Finally

I highly recommend avoiding non-English characters in file names when using PHP. It's damn hard to understand.

+2
source

You can simply check if file_exists() :

 if(file_exists('C:\Users\Administrator\Desktop\Wednesday read')) { // Do your thing... } 
+1
source

In a specific example, you can look at the problem differently:

What are the sources

 $dirs = scandir('C:\Users'); print_r($dirs); 

Since you know there is a folder called "Administrator" - see how php displays. Having received the result obtained by php, you can hope to determine the correct encoding in a specific folder. If the encoding is consistent (which corresponds to Vilx- , it is), then it should be possible to process any folders / files with Cyrillic characters.

+1
source

Do not use administrator rights!

Use UTF-8.

Use linux, at least in a virtual machine. This will save you a lot of time.

You should never rely on paths other than ASCII!

Use the file_exists () function to check if a file / directory exists: http://php.net/manual/en/function.file-exists.php

0
source

This problem may be caused by the fact that you are not β€œavoiding” backslashes, so PHP is trying to do this:

 is_dir('C:UsersDesktop ') 

What does not work.

Try to slip away from your slashes;

 is_dir('C:\\Users\\\\Desktop\\ ') 

Although using slashes also works on PHP on windows,

 is_dir('C:/Users//Desktop/ ') 
0
source

All Articles