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 目标计数模型训练过程中的问题
  • ¥100 Acess连接SQL 数据库后 不能用中文筛选
  • ¥15 用友U9Cloud的webapi
  • ¥20 电脑拓展屏桌面被莫名遮挡
  • ¥20 ensp,用局域网解决
  • ¥15 Python语言实验
  • ¥15 我每周要在投影仪优酷上自动连续播放112场电影,我每一周遥控操作一次投影仪,并使得电影永远不重复播放,请问怎样操作好呢?有那么多电影看吗?
  • ¥20 电脑重启停留在grub界面,引导出错需修复
  • ¥15 matlab透明图叠加
  • ¥50 基于stm32l4系列 使用blunrg-ms的ble gatt 创建 hid 服务失败