Can't use variable outside function even after setting it global
<p>I have made a script to generate thumbnails using PHP's GD library. The thing is that the original images are in their respective directories that are assigned to <code>'gallery/'.$section;</code>and the thumbs will go in the directory <code>'gallery/thumbs/'.$section</code> </p>

<p>Now, every time the script is executed i want it to check if the thumbnail already exist and if not only then create it but whenever i call the function and reload the page it creates new thumbnail every time.</p>

<p><strong>I know it is about the scope of the variable inside and outside the function but what it is I can't figure out. I've tried setting the variable as global but still it is not working out.</strong></p>

<p>Here is the pre-declaration. The <code>idir</code> is for images directory and the <code>tdir</code>is for thumbs directory.</p>

<pre><code>&lt;?php
$idir = "gallery/".$section.'/';
$tdir="gallery/thumbs/".$section.'/';
if(!file_exists($tdir)){
mkdir($tdir);
}
</code></pre>

<p>Here is the function i am using.</p>

<pre><code>function createThumbs($idir, $tdir, $tw, $th){
    $dir=opendir($idir);
    while(($fname = readdir($dir)) != false){
        if($fname!='.' &amp;&amp; $fname != '..'){
        $img = imagecreatefromjpeg($idir.$fname);

        $width = imagesx($img);
        $height = imagesy($img);

        if($width&gt;$height){
            $new_width=$tw;
            $new_height=$height*($tw/$width);
        }
        if ($width &lt; $height) 
        {
            $new_width=$width*($th/$height);
            $new_height=$th;
        }
        if ($width == $height) 
        {
            $new_width=$tw;
            $new_height=$th;
        }


        $tmp_img = imagecreatetruecolor($new_width, $new_height);
        imagecopyresampled($tmp_img, $img, 0,0,0,0, $new_width, $new_height, $width, $height);

        imagejpeg($tmp_img, $tdir.$fname, 100);

        imagedestroy($tmp_img); 
        imagedestroy($img); 
    }
}
closedir($dir); 
}
</code></pre>

<p>And after this i am calling the function like that.</p>

<pre><code>if (!file_exists($tdir.$fname)) {
    createThumbs($idir,$tdir,250, 200);
}
</code></pre>

<p>Also when calling the function it gives an error - <code>undefined variable .$fname</code>. </p>