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条)

报告相同问题?

悬赏问题

  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型