Beginner in PHP
Below is my code for retrieving files from a folder, but it is showing me the error
Undefined variable: files
Why i am getting this error.
It is an image gallery. Gallery folder does not have any image till now.
<?php
if($theRugz<>''){
/* settings */
$image_dir = 'gallery/'.$theRugz.'/';
$per_column = 5;
$count = 0;
if($theRugz=='11'){$rugType='Tufted';}else
if($theRugz=='12'){$rugType='Loom Knotted';}else
if($theRugz=='13'){$rugType='Indo Nepali';}else
if($theRugz=='14'){$rugType='Shaggy Carpets';}else
if($theRugz=='15'){$rugType='Cotton Kellims';}else
if($theRugz=='16'){$rugType='Hemp and Soumak';}else
if($theRugz=='17'){$rugType='Kellims Dhurries';}else
if($theRugz=='18'){$rugType='Cotton Rugs';}else
if($theRugz=='19'){$rugType='Bathmats';}else
if($theRugz=='21'){$rugType='Chindi Rugs';}else
if($theRugz=='22'){$rugType='Hand Knotted';}
if($_SESSION['SESS_CLIENT']==1){
/* Files for all */
/* step one: read directory, make array of files */
if ($handle = opendir($image_dir)) {
while (false !== ($file = readdir($handle)))
{
if ($file != '.' && $file != '..')
{
if(strstr($file,'-thumb') or strstr($file,'-tuhmb'))
{
$files[] = $file;
}
}
}
closedir($handle);
}
/* Step One End */
/* step two: loop through, format gallery */
if(count($files))
{
echo '<p class="head1">Range of '.$rugType.' (click on thumbnail to enlarge)</p>';
foreach($files as $file)
{
$count++;
if(strstr($file,'-tuhmb')){
echo '<a class="photo-link" rel="one-big-group" href="',$image_dir,str_replace('-tuhmb','',$file),'"><img src="',$image_dir,$file,'" width="183" height="130" alt="'.str_replace('-tuhmb.jpg','',$file).'" title="'.str_replace('-tuhmb.jpg','',$file).'" class="img-thumb" /></a>'.PHP_EOL;
}else{
echo '<a class="photo-link" rel="one-big-group" href="',$image_dir,str_replace('-thumb','',$file),'"><img src="',$image_dir,$file,'" width="183" height="130" alt="'.str_replace('-thumb.jpg','',$file).'" title="'.str_replace('-thumb.jpg','',$file).'" class="img-thumb" /></a>'.PHP_EOL;
}
if($count % $per_column == 0) { echo '<div class="clear"></div>'; }
}
}
else
{
echo '<p>There are no images in this gallery.</p>';
}
/* Step two End */
}else{
/* Files for all */
/* step one: read directory, make array of files */
if ($handle = opendir($image_dir)) {
while (false !== ($file = readdir($handle)))
{
if ($file != '.' && $file != '..')
{
if(strstr($file,'-thumb'))
{
$files[] = $file;
}
}
}
closedir($handle);
}
/* Step One End */
/* step two: loop through, format gallery */
if(count($files))
{
echo '<p class="head1">Range of '.$rugType.' (click on thumbnail to enlarge)</p>';
foreach($files as $file)
{
$count++;
echo '<a class="photo-link" rel="one-big-group" href="',$image_dir,str_replace('-thumb','',$file),'"><img src="',$image_dir,$file,'" width="183" height="130" alt="'.str_replace('-thumb.jpg','',$file).'" title="'.str_replace('-thumb.jpg','',$file).'" class="img-thumb" /></a>'.PHP_EOL;
if($count % $per_column == 0) { echo '<div class="clear"></div>'; }
}
}
else
{
echo '<p>There are no images in this gallery.</p>';
}
/* Step two End */
}
}
?>
$filesdoes not exist. It would help to know the line. You can use the code supplied above but it will probably not solve your issue. The problem could be related to using the wrong path to the folder or using a folder that is empty.