能够列出文件夹,但无法使用PHP列出文件

时间:2013-06-28 15:48:03

标签: php file directory

我正在尝试在我的文件夹目录中显示文件。我能够查看该文件夹,但不能查看该文件夹中的文件。

您能否确定我哪里出错了?

<?php

             $username=$_SESSION['username' ];
              if(isset($_GET['directory'])){
                  if(strcmp($_GET['directory'],' userFolders/')==0){
                        $dir='userFolders/'.$username. '/';
                  }else{
                         $dir=$_GET['directory'];
                  }

                $myfolders=myfolders($dir);
                if(empty($myfolders)){
                    echo '<tr><td> <b> Your folder is empty </b></td><td></td><td></td></ tr>';
                }else{
                    foreach($myfolders as $folder){
                                     echo '<tr>
                                                <td align="left"><a target=_blank href="honey.php?directory=',$ folder,'"> ',$folder,'</a></td>
                                                <td align="center"> --- </td>
                                                <td align="center"> <input type="image" src="delete.png" width="25" height="25"/></td>
                                          </tr>';
                    }
                        $dir_handle = @opendir($dir) or die("Unable to open $dir");
                        while ($file = readdir($dir_handle)) {

                                if($file == "." || $file == "..")
                                continue;

                                     echo '<tr>
                                                <td align="left"><a target="_blank" href="'.$file.'"> ',$file,'</a></td>
                                                <td align="center"> --- </td>
                                                <td align="center"> <input type="image" src="delete.png" width="25" height="25"/></td>
                                          </tr>';
                        }
                        closedir($dir_handle);
                }
              }
          }

2 个答案:

答案 0 :(得分:0)

这里有一个空格:$ folder和最后一个额外}。它应该是:$folder。尝试改变这一点,让我知道它是否有帮助。

答案 1 :(得分:0)

未定义函数myfolders(),第17行中的语句存在语法错误。你可能想要scandir()

我将其写为(已测试)

<?php
$username=$_SESSION['username' ];

if(isset($_GET['directory']))
{
    if(strcmp($_GET['directory'],' userFolders/')==0)
    {
    $dir='userFolders/'.$username. '/';
    }

else
{
$dir=$_GET['directory'];
}

$myfolders=scandir($dir);

if(empty($myfolders))
{
echo '<tr><td> <b> Your folder is empty </b></td><td></td><td></td></ tr>';
}
else
{
    foreach($myfolders as $folder)
    {
    echo '<tr>
    <td align="left"><a target=_blank href="honey.php?directory=$folder"> $folder</a></td>
    <td align="center"> --- </td>
    <td align="center"> <input type="image" src="delete.png" width="25" height="25"/></td>
    </tr>';
    }

    $dir_handle = @opendir($dir) or die("Unable to open $dir");

    while ($file = readdir($dir_handle)) 
    {
    if($file == "." || $file == "..")
    continue;
    echo '<tr>
    <td align="left"><a target="_blank" href="'.$file.'"> ',$file,'</a></td>
    <td align="center"> --- </td>
    <td align="center"> <input type="image" src="delete.png" width="25" height="25"/></td>
    </tr>';
    }
    closedir($dir_handle);
}

}

我希望这会有所帮助。