Some simple php code;

$dir = '../images/';
$file = $_GET['name']; // Last, First M. "nickname"
$f = $dir.$file;
list($width, $height, $type, $attr) = getimagesize($f);
echo $f."<br />";
echo "width ".$width."<br />";
echo "<img src='$f' $attr title='getimagesize example' />";

while the last echo line displays the image correctly (and the title) the gretimagesize function has a problem (No such file or directory). I'm fairly sure this is because the filename includes spaces. How can this be solved? Changing the actual file name is not possible (there are more than 23,0000!).

changing the file name is easy, php does STUFF

<?php 
///Posted in Phpadda.in/// 

// the directory  './' - is the current directory  '../' - is the parent directory 
// case sensitive 
// e.x.: /home/user or c:\files 
$dir='../images/'; 

// 1 - uppercase all files 
// 2 - lowercase all files 
// 3 - do not uppercase or lowercase 
$up=3; 

//rename files that have $w in their filename 
//case sensitive 
//set to '' if you want to rename all files 
$w='.jpg'; 

//do not rename files having $n in their filename 
$n=''; 

//add prefix to files 
$prefix=''; 

//add postfix 
$postfix=''; 

//replace 
// space with underscore
$replace=' '; 
$replace_with='_'; 

//true - traverse subdirectories 
//false - do not traverse subdirectories 
$tr=false; 

////// do NOT change anything below this ///////// 
set_time_limit(120); 
$files=array(); 
error_reporting(E_ERROR | E_PARSE); 
function prep($f,$dir) 
{ 
        global $up,$prefix,$postfix,$w,$replace_with,$replace,$n,$files; 
        if(strpos($f,$n)!==false) 
        return $f; 
        $f=str_replace($replace,$replace_with,$f); 
        if($up==1) 
        $f=strtoupper($f); 
        elseif($up==2) 
        $f=strtolower($f); 
        $f=$prefix.$f.$postfix; 
        $files[]=$dir.$f; 
        return $f; 
} 
$i=0; 
function dir_tr($dir) 
{ 
        global $i,$w,$tr,$files; 
        $dir=rtrim(trim($dir,'\\'),'/') . '/'; 
        $d=@opendir($dir); 
        if(!$d)die('The directory ' .$dir .' does not exists or PHP have no access to it<br><a target="_blank" href="http://phpadda.in">Need help?</a>'); 

        while(false!==($file=@readdir($d))) 
     { 
        if ($file!='.' && $file!='..' && $file!='renamer.php') 
        { 
            if(is_file($dir.$file)) 
            { 
                if($w=='' || (strpos($file,$w)!==false)) 
                { 
                    if(!in_array($dir.$file,$files)) 
                    { 
                        rename($dir.$file,$dir.(prep($file,$dir))); 
                        $i++; 
                    } 
                } 
            } 
            else 
            { 
                if(is_dir($dir.$file)) 
                { 
                    if($tr) 
                    dir_tr($dir.$file); 
                } 
            } 
        } 
    } 
    @closedir($d); 
} 
dir_tr($dir); 
echo '<br>Renamed ' . $i . ' files in directory ' . $dir; 
echo "<br>You can now delete renamer.php"; 
?>

lines above the
///// dont change anything below this ///
are config lines & basic instructions

<?php trim($filename); ?>

AB - your rename directory approached worked perfectly, and extremely fast...in a blink. However, I'm still getting the same error..file not found. I've check and the files are named correctly with underscores instead of spaces. The echo of $fnew looks exactly like the file name, and I DO see the correct image. I just can't get the image size info.

Here's my code.

$dir = '../image/';
    $file = $_GET['name'];  //                   Last, First M. "nickname"
    $f = $dir.$file;
    $fnew = str_replace(' ','_',$f); // ../image/Last,_First_M._"nickname"
    list($width, $height, $type, $attr) = getimagesize($fnew);
    $w = 780; // needed because $width is not defined
    echo $fnew."<br />";
    echo "width ".$width."<br />";	
    echo "<img src='$fnew' width='$w' title='".$file."'/>";
Member Avatar
diafol

shouldn't use quotes in fielnames as a rule. Why not use urlencode() to make filenames safe?

AB - your rename directory approached worked perfectly, and extremely fast...in a blink. However, I'm still getting the same error..file not found. I've check and the files are named correctly with underscores instead of spaces. The echo of $fnew looks exactly like the file name, and I DO see the correct image. I just can't get the image size info.

Here's my code.

$dir = '../image/';
    $file = $_GET['name'];  //                   Last, First M. "nickname"
    $f = $dir.$file;
    $fnew = str_replace(' ','_',$f); // ../image/Last,_First_M._"nickname"
    list($width, $height, $type, $attr) = getimagesize($fnew);
    $w = 780; // needed because $width is not defined
    echo $fnew."<br />";
    echo "width ".$width."<br />";	
    echo "<img src='$fnew' width='$w' title='".$file."'/>";

I just ran into something goofy this week, and I wonder if that has something to do with the problem you're having.

In my testing server (XP running Apache/PHP/MySQL) I had to have the directory fit the XP requirement

$dir ='..\\image\\';

but when I went to production on the server I had to change it to the way you have it.

Not sure if this is your problem, but it is a thought.

Solved....of course a dumb mistake by me. $file did not have a file extension. It was missing ".jpg".

I ASSumed that getimagesize() and the src in the <img> statement would both utilize the same variable. A file name is a filename, right? NOT! Apparently the <img> statement supplies a default ".jpg" if one is missing. Because the <img> statement worked without ".jpg" I failed to see my problem.

Corrected line:

list($width, $height, $type, $attr) = getimagesize($fnew.'.jpg');

I'll give almostBob the credit because I think removing spaces was an important part of the solution.

Member Avatar
diafol

Fair one.