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

报告相同问题?

悬赏问题

  • ¥100 X轴为分离变量(因子变量),如何控制X轴每个分类变量的长度。
  • ¥30 求给定范围的全体素数p的(p-2)的连乘积
  • ¥15 VFP如何使用阿里TTS实现文字转语音?
  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计