显示未定义变量的Php错误:文件,未知错误

时间:2015-04-17 08:33:42

标签: php file-handling

PHP初学者

以下是我从文件夹中检索文件的代码,但它显示错误

Undefined variable: files 

为什么我收到此错误。

这是一个图片库。 Gallery文件夹到目前为止还没有任何图像。

<?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 */
        }
    }

    ?>

2 个答案:

答案 0 :(得分:1)

$files is undefined表示您使用的是您未定义的变量(显然)。

通过简要地查看代码,我可以看到变量files正在循环$files[] = $file;中使用。你应该问问自己,为什么这个变量是未定义的?

答案非常简单 - 循环或条件永远不会运行。 为了执行上述行,您的代码应满足4个条件(3 ifwhile循环)。

if ($handle = opendir($image_dir)) {
                while (false !== ($file = readdir($handle))) 
                {
                    if ($file != '.' && $file != '..') 
                    {
                        if(strstr($file,'-thumb') or strstr($file,'-tuhmb'))
                        {

跟踪您的代码并找到返回false的条件,并阻止您的代码进入$files[] = ...行。

无论如何,在代码开头声明该变量将防止出现该错误。由于您将其用作数组:

$files = array();

答案 1 :(得分:0)

这不会解决问题,但会使您的代码更容易维护。

为这些rugtypes创建一个数组

$rugtType = '';
$rugTypes = array(
        '11' => 'Tufted',
        '12' => 'Loom Knotted',
        '13' => 'Indo Nepali', 
        '14' => 'Shaggy Carpets', 
        '15' => 'Cotton Kellims',
        '16' => 'Hemp and Soumak',
        '17' => 'Kellims Dhurries',
        '18' => 'Cotton Rugs',
        '19' => 'Bathmats',
        '21' => 'Chindi Rugs',
        '22' => 'Hand Knotted');

 $rugType = $rugTypes[$theRugz];
相关问题