PHP blank and special characters error of filenames

<div id="gallery">


$directory = 'people';


foreach (glob($directory.'/*.{jpg,jpeg,png,gif}', GLOB_BRACE) as $file){
    if($file=='.' || $file == '..') continue;
    $file_parts = explode('.',$file);
    $ext = strtolower(array_pop($file_parts));
    $title = basename($file);
    $title = htmlspecialchars($title);
    echo '
    <div class="pic '.$nomargin.'" style="background:url(thumbs/'.$file.') no-repeat 50% 50%;">
    <a href="'.$file.'" title="whatever you want : '.$title.'" rel="pan1" target="_blank">'.$title.'</a>

<div class="clear"></div>

This is my code sample for a dynamic image gallery. I shows all images in a directory automatically with thumbnails from another folder. Here is my problem: If I use a blank char or a special characters in file name then it doesn't show the file at all. I want to use blanks and special characters such as (ç, ö, ş) because I want to use image title as a description of the image without having to write any extra text. Any idea how to do this? Thanks.

Sample :


I'll admit I don't know all characters which are and aren't valid in filenames on different operating systems and filesystems, but I do know that some can be problematic, and that it depends on the platform. My personal recommendation is to just avoid the whole issue.

Either store the file meta data(in your case, the title) in a database and then associate each title to some serial numbered file like 159.jpg, or consider encoding the filename down to a small subset of ascii, as yielded by urlencode()

eg, store ç.jpg urlencoded as %C3%A7.jpg and then just urlDecode() for display...

beware of hitting the maximum file name, and possibly file path lengths. They too vary per platform. This is another point for using a database, as it avoids long filenames.

Need Your Help

Magento, url duplication , seo

magento url seo duplicates

I have a website in magento and i am facing a problem that there are 2 url that are being generated for same product and that is creating problem for the SEO of that website.