0

I have some PHP Code and I am getting an Error when I use chdir to work with Hebrew file names:

function GetSubFoldersArray()
{
    $subFoldersArr = array();

    $yourStartingPath = "images";
    $iterator         = new RecursiveIteratorIterator(
        new RecursiveDirectoryIterator($yourStartingPath),
        RecursiveIteratorIterator::SELF_FIRST);

    foreach ($iterator as $file) {
        if ($file->isDir()) {
            $path = strtoupper($file->getRealpath());

            $path2 = PHP_EOL;

            $path3 = $path . $path2;

            $result = end(explode('/', $path3));

            array_push($subFoldersArr, $result);
        }
    }

    return $subFoldersArr;
}


$subFolders = GetSubFoldersArray();
// $response["images_arr"] = array();
$arrlength = count($subFolders);
chdir("images");

for ($x = 0; $x < $arrlength; $x++) {
    echo $subFolders[$x];
    echo "<br>";
    echo getcwd();
    echo "<br>";
    chdir($subFolders[$x]);
}

The subfolder is ../images/ which have Hebrew characters in them.

I was able to extract the subfolder's hebrew file names and put them all in an array. When I'm looping through the array, I'm trying the set the subfolder name using the chdir() function, but it fails with:

Warning: chdir() [function.chdir]: No such file or directory (errno 2) in 
/home/a2056935/public_html/android_connect/loopingDir.php on line 44.

The strange thing is that when I manually enter the subfolder name:

chdir("מקום2") 

then it works fine. But when I try to go through the prev. created sub folder array, it fails.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Alex Batt
  • 65
  • 1
  • 2
  • 8
  • 1
    As an Israeli programmer, I've never ever found the need to name my files in Hebrew. I'm interested to know why you did. – Madara's Ghost Aug 17 '13 at 10:15
  • What editor are you using to write commands with Hebrew characters (which are still ASCII btw)? In notepad++ for instance you have to make sure you're using UTF-8 encoding – Itay Aug 17 '13 at 10:17
  • Thanks for the prompt reply, I'm using notepad++ with UTF8 defined. – Alex Batt Aug 17 '13 at 10:22
  • The need for Hebrew sub folders names is because each folder represents a place name, which can be in Hebrew/ English or any other language. each folder containing pictures of that certain place. – Alex Batt Aug 17 '13 at 10:24
  • You said when you go on the list "it fails", how? Are you just checking the list of the echoed subfolders? – Itay Aug 17 '13 at 10:26
  • Not sure that I filly understood your reply. I have a function that creates an array of sub folders name. In the main function, I'm trying to access these subfolders, using the names from the array. For example, if my cwd is ../images and the sub folder is array[0] = מקום, my expectation and chdir($array[0]) will take me to ../imames/מקום. but this fails. – Alex Batt Aug 17 '13 at 10:34
  • Yes, I'm checking the list of the echoed subfolders. I've also checked the encoding for the sub folders names and they are UTF8 – Alex Batt Aug 17 '13 at 10:59

1 Answers1

1

The error is in these two lines:

$path2 = PHP_EOL;
$path3 = $path . $path2;

On windows, the PHP Predefined Constant PHP_EOL has the value \r\n. On a MAC it is \r, and on Linux it is \n.

The array does not contain the names of the subfolders, but the names followed by a EOL character. Therefore chdir fails.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Lorenz Meyer
  • 19,166
  • 22
  • 75
  • 121