【问题标题】:Empty Directory Error Handling空目录错误处理
【发布时间】:2011-08-25 08:34:53
【问题描述】:

我想知道如何做一个基本的 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);
  }

【问题讨论】:

  • 这只是一个通知而不是 php 错误。 PHP 抱怨变量 $thelist,它直接用于连接字符串:@$thelist .= '...';为防止出现此通知,只需在定义变量的位置添加一行:$thelist = '';
  • 是的...添加 $thelist='';就在 mysql_query 之后,警告将消失
  • @Develman 谢谢!但是我如何对其进行编码以显示“数据库中没有文件”?我应该使用 TryCatch 否?

标签: php error-handling directory try-catch


【解决方案1】:

“$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 行

【讨论】:

  • 但是如果目录为空,我该如何编码以显示“数据库中没有文件”?
  • 您的代码不起作用(它没有显示消息),所以我尝试对其进行一些修改。但它仍然无法正常工作 (pastebin.com/n2w8Xe1S)
  • ssory。在第 26 行: if($thelist==''){ $thelist = '

    否数据库中的文件

    '; }
  • 因此显示了错误消息,但我在 14 和 15 Warning: scandir(./templates/,./templates/) [function.scandir]: The system cannot find the file specified. (code: 2) in C:\xampp\htdocs\viewer\list.php on line 14 Warning: scandir(./templates/) [function.scandir]: failed to open dir: No such file or directory in C:\xampp\htdocs\viewer\list.php on line 14 Warning: scandir() [function.scandir]: (errno 2): No such file or directory in C:\xampp\htdocs\viewer\list.php on line 14 Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\viewer\list.php on line 15 上遇到了新错误
  • 对不起,我忘了更改路径。新错误是Notice: Undefined variable: file in C:\xampp\htdocs\viewer\list.php on line 18 Notice: Undefined variable: file in C:\xampp\htdocs\viewer\list.php on line 18,但不显示错误消息
【解决方案2】:

在你应该添加之前

$thelist = ''; 

也许甚至在 if 之前 :)

为了捕捉异常你应该放

throw new Exception(....);

在 try..catch 块内。

【讨论】:

  • 我试过了,正如我在原始问题的代码中向您展示的那样。但是它不起作用
  • 你应该向上移动 try { 并在 if 下设置。否则我不明白需要在哪里显示此消息。
猜你喜欢
  • 2017-10-29
  • 2018-04-17
  • 2017-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多