duanlang1196 2015-04-17 08:33
浏览 74
已采纳

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

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

    ?>
  • 写回答

2条回答 默认 最新

  • dongpai9986 2015-04-17 08:45
    关注

    $files is undefined means that you use a variable that you didn't defined (obviously).

    By looking briefly at your code I can see that the variable files is being used inside a loop $files[] = $file;. You should ask yourself, why is this variable is undefined?

    The answer is pretty much simple - the loop or the condition never runs. In order for the mentioned above line to get executed your code should fulfill 4 conditions (3 if and a while loop).

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

    Track your code and locate the condition that returns false and preventing your code from getting into the $files[] = ... line.

    Anyway, declaring that variable at the beginning of the code will prevent that error from appearing. Since you're using it as an array:

    $files = array();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂