Search code examples
phpencodingsymbolsentitiesscandir

problems with symbols (scandir, array_diff, foreach)


I'm trying to write a script which open the directory, scan all files (images) and displayed it on the screen, but the problem is, it contains symbols in their names and scandir can't handle it.

It's ok with the directories where no symbols are present in their names, the script works perfect, but the directories/files with the symbols in their name is a big problem.

For instance, I have a total 2 directories and all files in that directories contains the same symbols in their name. One contain "®" and the other contain "™" and every single file (image) have these symbol in their names, for example: "Right® screen452217.jpg". So, renaming the directories and files is not an option, this would take too much time.

Maybe is there a better way, but I do it directly from url on screen with $_GET. It's on a local server for now, I'm working in VertrigoServ WAMP server and the script then look like:

http://127.0.0.1/screenshots/index.php?page=Rights®
http://127.0.0.1/screenshots/index.php?page=Trade%20Marks™

$url = "screens/".$_GET["page"]."";
$scan = scandir($url, SCANDIR_SORT_NONE);
$scaned = array_diff($scan, array('.', '..', 'Thumbs.db'));

foreach($scaned as $shots) {

    if ($shots <> "thumbnails") {

echo "<li>".PHP_EOL.
     " <img width=\"253\" height=\"158\" src=\"".$url."\\".$shots."\"".
     "  alt=\"".$shots."\" title=\"".$shots."\".
     " </li>".PHP_EOL.PHP_EOL;

    }

}

scandir reports these errors:

The system cannot find the file specified. (code: 2)
failed to open dir: No such file or directory
No such file or directory

array_diff reports this error:

Argument #1 is not an array

and finally foreach report:

Invalid argument supplied for foreach()

I also tried to change encoding with mb_convert_encoding() and iconv(), but no luck.

My best result was after changing url, instead of using "®" or "™" I used windows-1252 ASCII Encoding Reference:

http://127.0.0.1/screenshots/index.php?page=Rights%AE
http://127.0.0.1/screenshots/index.php?page=Trade%20Marks%99

Then everything passed with no error, but instead of showing images, the "img src" looks like:

screens/Rights�\Right�452217.jpg

which is still problem, because images are not displayed. I like to stick with scandir, because I already have done the page, but I can't pass through this problems with symbols. Can you help me, please ?


Solution

  • It was actually simpler than I thought. Problem was, that the directory was already with symbols in their names, so I must change that to Windows-1252 like this:

    $page = $_GET["page"];
    
    if (strpos($page, '®')) {$page = str_replace('®', '%AE', $page);}
    if (strpos($page, '™')) {$page = str_replace('™', '%99', $page);}
    

    Then the URLs change from:

    http://127.0.0.1/screenshots/index.php?page=Rights®
    http://127.0.0.1/screenshots/index.php?page=Trade%20Marks™
    

    To:

    http://127.0.0.1/screenshots/index.php?page=Rights%AE
    http://127.0.0.1/screenshots/index.php?page=Trade%20Marks%99
    

    Then it was simple, just add encoding after scandir and one in foreach:

    $url = "screens/".$_GET["page"]."";
    $scan = scandir($url, SCANDIR_SORT_NONE);
    $scaned = array_diff($scan, array('.', '..', 'Thumbs.db'));
    
    $url = iconv("windows-1252", "UTF-8", $screen_uplay);
    
    foreach($scaned as $shots) {
    
        if ($shots <> "thumbnails") {
    
        $shots = iconv("windows-1252", "UTF-8", $shots);
    
    echo "<li>".PHP_EOL.
         " <img width=\"253\" height=\"158\" src=\"".$url."\\".$shots."\"".
         "  alt=\"".$shots."\" title=\"".$shots."\".
         " </li>".PHP_EOL.PHP_EOL;
    
        }
    
    }
    

    Simple as that. Maybe is there a better way, but this is working for me and I'm ok with that.