Traditional Chinese Big5 directory name display failure...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dc2698
Forum Newbie
Posts: 7
Joined: Fri Jan 05, 2007 1:53 am

Traditional Chinese Big5 directory name display failure...

Post by dc2698 »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi all,

Recently I am trying a free php gallery called Saurdo Gallery.  I installed it successful and it is working well.  It reads all defined directories and files in them and displays the directory names as well as all the images inside these directories.

Problem occurred when I tried to change a directory name from English to Traditional Chinese Big5.  The renamed directory didn't show up in the gallery index page.

I tried the following but [color=red]no success[/color]:
in showDirs() function:

Code: Select all

$Dirs = buildDirs(unicode_encode($gallery_root.$currentdir,'BIG5'));
$Dirs = buildDirs(unicode_decode($gallery_root.$currentdir,'BIG5'));
$Dirs = buildDirs(utf8_encode($gallery_root.$currentdir));
$Dirs = buildDirs(utf8_decode($gallery_root.$currentdir));
$Dirs = buildDirs(htmlspecialchars($gallery_root.$currentdir,ENT_QUOTES,BIG5));
$Dirs = buildDirs(htmlentities($gallery_root.$currentdir,ENT_QUOTES,BIG5));
buildDirs() is defined in the code section.

Thank you very much in advance!

Code: Select all

<?php

// Globals - Do not tamper with these
global $gallery_root, $gallery_address, $file, $excluded_folders, $pictwidth, $pictheight, $thumbwidth, $thumbheight, $gallery_width;

// ---------- Settings ----------
// Configure these settings, to use the gallery.

// Your gallery folder (this is where your pictures and picture folders are located).
$gallery_address = '/images/photos/';


// Add foldernames to exclude. (add more lines like this on more excludes.)
$excluded_folders[] = 'cgi-bin';

// Picture Preview size
$pictwidth = 600;
$pictheight = 600;

// Thumbnail size
$thumbwidth = 200;
$thumbheight = 200;
// ---------- Settings end ----------

$gallery_root = $_SERVER['DOCUMENT_ROOT'].$gallery_address;
$gallery_address = 'http://'.$_SERVER['HTTP_HOST'].$gallery_address;

function showGallery() {
    global $file;
    
    if (!validateFile())
    {
        echo 'Invalid folder or file';
        return;
    }
    createNavigation();

    $path = pathinfo($file);
    if ($path['extension'] == '')
    {
    //Display Dir(s) (if any)
    showDirs();

    //Display Thumb(s) (if any)
    showThumbs();

    } else {
    showSlide($file);

    }
}

function setCurrentdir()
{
    global $currentdir, $file;
    $path = pathinfo($file);
    if ($path['extension'] != '')
        $currentdir = $path['dirname'].'/';
    else
        $currentdir = $file;
}

function [color=red]buildDirs[/color]($Dir) {
    if ( substr ( $Dir, -1 ) != '/' ) $Dir .= '/'; // Add a trailing slash
    $Handle = opendir($Dir);
    $Dirs = array();
    while ( false !== ( $File = readdir ( $Handle ) ) )
    {
        if ( $File == '.' OR $File == '..' OR !is_dir ( $Dir . $File ) ) continue;
        $Dirs[] = $File;
    }
    natcasesort ( $Dirs );
    return $Dirs;
}

function [color=red]showDirs()[/color]{
    global $gallery_root, $currentdir, $file, $excluded_folders;
    $Dirs = buildDirs($gallery_root.$currentdir);
    $runonce = false;
    foreach ( $Dirs as $dir ) {
        if (!in_array($dir, $excluded_folders)) {
            if ( !$runonce ){
                echo '<div class="folder">Folders:<br/><table class="foldersbox">';
                $runonce = true;
            }
            echo '<tr><td><a href="'.$_SERVER['PHP_SELF'].'?file='.$currentdir.$dir.'/"><img src=./gallery/images/folder.gif> '.$dir.'</a></td></tr>';
        }
    }
    if ( $runonce )
        echo '</table></div><br/>';
}

///// Show Slide
function showSlide($slidefile) {
    
    global $gallery_root, $gallery_address, $currentdir, $file;
    if ($dir_content = opendir($gallery_root.$currentdir)) {
        while ( false !== ($img = readdir($dir_content)) ) {
            if ( is_file($gallery_root.$currentdir.$img) && eregi(".*(\.jpg|\.gif|\.png|\.jpeg)", $img))
                    $imgfiles[] = $img;
        }
    }

    $slide = '';

    $arraysize = count($imgfiles);
    
    for ($i=0; $i < $arraysize; $i++)
    {
    
        if($currentdir.$imgfiles[$i] == $slidefile)    
        {
            $slide = $imgfiles[$i];              
        }
    }
    
    echo '<div class="image"><a href="'.$gallery_address.$currentdir.$slide.'" target="_new"><img src="./gallery/img.php?file='.$currentdir.$slide.'&thumb=0"></a>
        </div>
        <br/>
        <div class="imgdata"><br/>';

///////////// This is the Exif section
$exif = exif_read_data($gallery_root.$currentdir.$slide, 0, true);
                echo "<ul>";
                // get file name
                echo "<li>Name: <b>" . $exif['FILE']['FileName'] . "</b></li>";
				// get file size
				if ($exif['FILE']['FileSize'] == null){
				echo '';
				}else{
                echo "<li>File Size: " . floor($exif['FILE']['FileSize']/10.24)/100 . " kbs";
				}
                // get image dimensions
				if ($exif['COMPUTED']['Width'] == null || $exif['COMPUTED']['Height'] == null){
				echo '';
				}else{
                echo "<li>Dimensions: " . $exif['COMPUTED']['Width'] . " x " . $exif['COMPUTED']['Height'] . " </li>";
				}
				// get timestamp
				if($exif['EXIF']['DateTimeOriginal'] == null){
				echo ''; 
				}else{
                echo "<li>Timestamp: "  . $exif['EXIF']['DateTimeOriginal'] . "</li>";
				}
				// get exposure
				if($exif['EXIF']['ExposureTime'] == null){
				echo ''; 
				}else{
                echo "<li>Exposure Time: "  . $exif['EXIF']['ExposureTime'] . "th of a second </li>";
				// get Aperture
				if($exif['EXIF']['ApertureValue'] == null){
				echo ''; 
				}else{
                echo "<li>Aperture: "  . $exif['EXIF']['ApertureValue'] . "</li>";
				}
				// get Flash
				if($exif['EXIF']['Flash'] == null){
				echo ''; 
				}else{
                echo "<li>Flash: "  . $exif['EXIF']['Flash'] . "</li>";
				}
				// get Focal Length
				if($exif['EXIF']['FocalLength'] == null){
				echo ''; 
				}else{
                echo "<li>Focal Length: "  . $exif['EXIF']['FocalLength'] . "</li>";
				}
				// get Digital Zoom
				if($exif['EXIF']['DigitalZoomRatio'] == null){
				echo ''; 
				}else{
                echo "<li>Digital Zoom: "  . $exif['EXIF']['DigitalZoomRatio'] . "</li>";
				}
				// get ISO Speed
				if($exif['EXIF']['ExposureIndex'] == null){
				echo ''; 
				}else{
                echo "<li>ISO Speed: "  . $exif['EXIF']['ExposureIndex'] . "</li>";
				}
				// get White Balance
				if($exif['EXIF']['WhiteBalance'] == null){
				echo ''; 
				}else{
                echo "<li>White Balance: "  . $exif['EXIF']['WhiteBalance'] . "</li>";
				}
				}
//////////end exif
				'
		</ul></div>';

}

//// Displaying thumbs
function showThumbs() {
    
    global $gallery_root, $gallery_address, $currentdir, $file, $thumbwidth, $thumbheight;
	
    
    if ($dir_content = opendir($gallery_root.$currentdir)) {
        while ( false !== ($file = readdir($dir_content)) ) {
            if ( is_file($gallery_root.$currentdir.$file) )
                if(eregi(".*(\.jpg|\.gif|\.png|\.jpeg)", $file))
                    $imgfiles[] = $file;
        }
    }
	
    echo '<div class="image">Images<p>';
    if(isset($imgfiles))
    {
	sort($imgfiles);
        foreach ($imgfiles as $img)
        {
            //echo '<div style="position: relative; width: '.($thumbwidth+16).'px; height: '.($thumbheight+16).'px; border: 1px solid #a9a9a9;">';
            echo '<a href="./gallery/img.php?file='.$currentdir.$img.'&thumb=0" class="linkopacity" rel="lightbox['.$currentdir.']" title="View Preview" id="<ul><li>Full sized - <a href='.$gallery_address.$currentdir.$img.' target=_blank>'.$img.'</a></li><li><a href='.$_SERVER['PHP_SELF'].'?file='.$currentdir.$img.' target=_blank>Detailed information</a></li></ul>"><img src="./gallery/img.php?file='.$currentdir.$img.'&thumb=1"></a>';
            //echo '</div>';
        }
    }
    echo '</div>';
}

// Validates file variable
function validateFile() {

    global $excluded_folders, $file;
    $file = $_GET['file'];
    
    // validate dir
    if ( strstr($file, '..') || strstr($file, '%2e%2e') )
        return false;
        
    foreach ($excluded_folders as $folder)
    {
        if ( strstr($file, $folder) )
        return false;
    }
    setCurrentdir();
    return true;
}

function createNavigation()
{
    global $currentdir, $file;

    if ($currentdir == './')
    $currentdir = '';
    $nav = split('/', $currentdir);
    array_pop($nav);
    $path = pathinfo($file);
    
    echo '<div>Now Viewing: &raquo; <a href="'.$_SERVER['PHP_SELF'].'">Gallery</a> ';
    foreach ($nav as $n)
    {
        $current .= $n.'/';
        echo '&raquo; <a href="'.$_SERVER['PHP_SELF'].'?file='.$current.'">'.$n.'</a> ';
    }
    if ($path['extension']!='')
        echo '&raquo; <a href="'.$_SERVER['PHP_SELF'].'?file='.$current.$path['basename'].'">'.$path['basename'].'</a>';
    echo '</div><br>';
}
?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

Use PHP tags instead of CODE tags for your code.

Also, nobody is going to read through all of that and try to decipher it for you, you should start with some basic troubleshooting and narrow down where the code is failing, then post that snippet.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Wikipedia seems to say: Big5 isn't unicode and every character is 2-bytes

What happens when you call buildDirs() etc. without using those unicode functions on the data?
Also, nobody is going to read through all of that and try to decipher it for you, you should start with some basic troubleshooting and narrow down where the code is failing, then post that snippet.
Yeah just remove the function we don't need to look at like showSlide().
dc2698
Forum Newbie
Posts: 7
Joined: Fri Jan 05, 2007 1:53 am

Ask again...

Post by dc2698 »

Sorry, this was my first post here :p

Maybe I should have started by asking a more general question:

How do PHP directory functions like opendir() and readdir() handle Traditional Chinese BIG5 (950) characters in physical directory names please? Any non-unicode encoding/decoding is needed before using above directory functions?

Is it having something to do with the system (Linux) regional and language settings in advance?

Thanks again!
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

How do PHP directory functions like opendir() and readdir() handle Traditional Chinese BIG5 (950) characters in physical directory names please?
Yes that is a better question. PHP in general handles all strings in a binary-safe way making one the assumption that one character equals one byte. When this is not true you can still make use of the fact that you can manipulate strings on a byte level.

But in your case that is only half the story because opendir() and readdir() are gateways to the filesystem and I've no idea what ext2 or 3 is capable of.
Is it having something to do with the system (Linux) regional and language settings in advance?
Not really they don't do a great deal besides choosing what symbol to use for decimal point and thousand separator etc.

Lastly, no unicode function is going to help you here because big5 isn't unicode :P
Post Reply