1

I need to work with files having accented characters in their name. Unfortunately, it looks like GLOB ignores these files. I have created this short code for the demonstration:

$files = glob(dirname(__FILE__) .  "/data/tracks/167/*.*");

foreach($files as $file) {
    var_dump($file);
    var_dump(file_exists($file));
}

Output is:

D:\data\zdroje\Kombinovane\MotoQuest\Web\admin\pokus.php:13:string 'D:\data\zdroje\Kombinovane\MotoQuest\Web\admin/data/tracks/167/162_P1030721 (kopie).jpg' (length=87)
D:\data\zdroje\Kombinovane\MotoQuest\Web\admin\pokus.php:14:boolean true
D:\data\zdroje\Kombinovane\MotoQuest\Web\admin\pokus.php:13:string 'D:\data\zdroje\Kombinovane\MotoQuest\Web\admin/data/tracks/167/162_tn_P1030721 (kopie).jpg' (length=90)
D:\data\zdroje\Kombinovane\MotoQuest\Web\admin\pokus.php:14:boolean true
D:\data\zdroje\Kombinovane\MotoQuest\Web\admin\pokus.php:13:string 'D:\data\zdroje\Kombinovane\MotoQuest\Web\admin/data/tracks/167/1_1493222210_P1030721.jpg' (length=88)
D:\data\zdroje\Kombinovane\MotoQuest\Web\admin\pokus.php:14:boolean true
D:\data\zdroje\Kombinovane\MotoQuest\Web\admin\pokus.php:13:string 'D:\data\zdroje\Kombinovane\MotoQuest\Web\admin/data/tracks/167/1_1493222210_tn_P1030721.jpg' (length=91)
D:\data\zdroje\Kombinovane\MotoQuest\Web\admin\pokus.php:14:boolean true

My directory contains 6 files - the 2 with accented characters are ignored.:

enter image description here

The same is with file_exists, even, when I use hints listed here - PHP file_exists with accent returns false.

One of the filenames is:

1_1493385948_tn_22-Ještěd21.jpeg

Any idea, what's wrong?

Community
  • 1
  • 1
user3523426
  • 836
  • 3
  • 11
  • 26
  • could you copy that filename into the description, my first attempt of getting the same behaviour failed (i used the str twô and this worked) – sics Oct 14 '17 at 08:02

1 Answers1

0

Apologies, not enough rep to comment. Can you tell me what you get from:

var_dump( iconv_get_encoding() );

And how about:

$files = scandir( dirname(__FILE__) . "/data/tracks/167/" );
var_dump( $files );

Can you use:

<?php
$files = array_filter( 
    scandir( dirname(__FILE__) ), function( $file_or_folder ) {
        return !is_dir( dirname(__FILE__) . $file_or_folder );
    }
);
var_dump( $files );
?>

Instead of glob?

Erik
  • 384
  • 1
  • 9
  • I get: array (size=3) 'input_encoding' => string 'UTF-8' (length=5) 'output_encoding' => string 'UTF-8' (length=5) 'internal_encoding' => string 'UTF-8' (length=5) – user3523426 Oct 14 '17 at 12:54
  • That is what I get as well, so not an encoding issue... Please note that the extension JPEG as opposed to JPG is also different for the accented filenames... BTW your code works on my system. – Erik Oct 14 '17 at 13:09
  • As I am using *.* for the GLOB command, there should be no difference-. I just renamed JPEG to JPG and the result is same .... Probably it has something to do with OS code page - I use Windows 10 64bit code page 1250. On other hand, it shouldn't matter for PHP. – user3523426 Oct 14 '17 at 14:26
  • It looks like the filenames are exactly CP1250. On other hand it doesn't explain, why glob doesn't find them. The outpurt is array (size=8) 0 => string '.' (length=1) 1 => string '..' (length=2) 2 => string '162_P1030721 (kopie).jpg' (length=24) 3 => string '162_tn_P1030721 (kopie).jpg' (length=27) 4 => string '1_1493222210_P1030721.jpg' (length=25) 5 => string '1_1493222210_tn_P1030721.jpg' (length=28) 6 => string '1_1493385948_22-Jes�te�d21.jpg' (length=30) 7 => string '1_1493385948_tn_22-Jes�te�d21.jpg' (length=33) – user3523426 Oct 15 '17 at 09:11
  • And there the files are, do you need the glob regex functionality? – Erik Oct 15 '17 at 09:21
  • Can you use my latest addition to my answer? – Erik Oct 15 '17 at 09:36
  • ERIK - sorry, I cannot - I have to work with files like copy, unlink. So I need PHP to recognize the files. – user3523426 Oct 16 '17 at 10:28
  • That makes little sense to me, neither copy or unlink require glob to find your file, especially if scandir can find your file. – Erik Oct 16 '17 at 11:03
  • Only thing I can think of it's an Operating System issue, though it works on my local win10 xampp install and my ubuntu server string(36) "1_1493385948_tn_22-Ještěd21.jpeg" – Erik Oct 16 '17 at 11:11