php - 空目录错误处理

标签 php error-handling directory try-catch

我想知道如何执行基本的Try Catch块,如果目录中没有文件,则该块会打印一条消息

这是我尝试的:

<?php

$dbhost = 'localhost';
$dbuser = 'admin';
$dbpass = 'root';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

$dbname = 'files';
mysql_select_db($dbname);


mysql_query("TRUNCATE files_tbl");

 if ($handle = opendir('./pdf/')) {



   while (false !== ($file = readdir($handle)))
      {


          if ($file != "." && $file != "..")   {

          if ( ($file = @scandir('./pdf/') && (count($file) > 0) ) ) {

          $directory_not_empty = TRUE;
          } {
                throw new Exception('No files in database');
        } try {

            @$thelist .= '<font face="Calibri" size="4"><p><b><img src="images/document.png"> '.$file.'<br></b></p></a></font>';

            $query = "INSERT INTO files.files_tbl(Name) VALUES ('$file');";


            mysql_query($query) or die('Error, Insert query failed' . mysql_error());

          } catch (Exception $e) {
                die ($e->getMessage);


       }

       }

}

  closedir($handle);
  }


?>

<html>
    <head>
    <link rel="stylesheet" href="style.css" type="text/css">
        <title>Search the Database</title>
    </head>

    <body>

    <div align="right"><form action="search.php" method="post">
    <div align="left"><font face="Arial" size="8" ><b>Document Database</b> </font></div>
     <font face="Arial" size="5px" ><b> Search: <input type="text" name="term" style="border:1px dashed #216DAB"/></b></font>

    &nbsp;

    <input type="image" src="images/search1.png"  name="submit" value="Search" align="right"/>
    </form>
    <hr size="20" noshade="noshade" color="#216DAB">
    </div>
    </body>
</html>

<P><font face="Arial" Size="6px" color="black" ><b>List of files</b></font></p>
<P><center><table border="0" bgcolor="#E0E0E0" style="border:1px dotted black"><td><?=$thelist?></td></table></center></p>

但是,我在加载php页面时收到PHP错误。
Notice: Undefined variable: thelist in C:\xampp\htdocs\viewer\list.php on line 76

TryCatch部分:
while (false !== ($file = readdir($handle)))
      {


          if ($file != "." && $file != "..")   {

          if ( ($file = @scandir('./pdf/') && (count($file) > 0) ) ) {

          $directory_not_empty = TRUE;
          } {
                throw new Exception('No files in database');
        } try {

            @$thelist .= '<font face="Calibri" size="4"><p><b><img src="images/document.png"> '.$file.'<br></b></p></a></font>';

            $query = "INSERT INTO files.files_tbl(Name) VALUES ('$file');";


            mysql_query($query) or die('Error, Insert query failed' . mysql_error());

          } catch (Exception $e) {
                die ($e->getMessage);


       }

       }

}

  closedir($handle);
  }

最佳答案

未定义“$ thelist”变量

<P><center><table border="0" bgcolor="#E0E0E0" style="border:1px dotted black"><td><?=$thelist?></td></table></center></p>

看看那里http://pastebin.com/jPdeieQV
参见第13、31和76行

关于php - 空目录错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7187318/

相关文章:

php - Laravel 使用具有模式的表创建模型类

php - 我们可以使用这个简单的方法而不是使用变量本身吗

php - 没有教义的 Symfony2 身份验证(登录)?

python - AIOHTTP Server - 用于引发条件的 Jinja 模板

php - 修复所有警告,注意,错误是否重要?

go - 如何检测自定义错误

java - 使用 "My files"应用程序单击按钮打开文件夹

php - 如何修复以获取 MySql 中的特定数据

PHP:使用相对图像路径从指定目录中提取所有图像

c - 如何获取C中目录中所有文件的总字节数