Pergunta

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.

Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top