Обработка ошибок в пустой директории - PullRequest
0 голосов
/ 25 августа 2011

Я хотел бы знать, как сделать основной блок 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);
  }

Ответы [ 2 ]

2 голосов
/ 25 августа 2011

Вариант "$ 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

0 голосов
/ 25 августа 2011

До того как вы добавите

$thelist = ''; 

Может быть, даже раньше, если:)

Для отлова исключения вы должны поставить

throw new Exception(....);

Внутри блока try..catch.

...