Note that there are some explanatory texts on larger screens.

plurals
  1. POEmpty Directory Error Handling
    primarykey
    data
    text
    <p>I would like to know how to do a basic Try Catch block that prints a message if there are no files in a directory</p> <p>This is what I tried:</p> <pre><code>&lt;?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 != "." &amp;&amp; $file != "..") { if ( ($file = @scandir('./pdf/') &amp;&amp; (count($file) &gt; 0) ) ) { $directory_not_empty = TRUE; } { throw new Exception('No files in database'); } try { @$thelist .= '&lt;font face="Calibri" size="4"&gt;&lt;p&gt;&lt;b&gt;&lt;img src="images/document.png"&gt; '.$file.'&lt;br&gt;&lt;/b&gt;&lt;/p&gt;&lt;/a&gt;&lt;/font&gt;'; $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-&gt;getMessage); } } } closedir($handle); } ?&gt; &lt;html&gt; &lt;head&gt; &lt;link rel="stylesheet" href="style.css" type="text/css"&gt; &lt;title&gt;Search the Database&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;div align="right"&gt;&lt;form action="search.php" method="post"&gt; &lt;div align="left"&gt;&lt;font face="Arial" size="8" &gt;&lt;b&gt;Document Database&lt;/b&gt; &lt;/font&gt;&lt;/div&gt; &lt;font face="Arial" size="5px" &gt;&lt;b&gt; Search: &lt;input type="text" name="term" style="border:1px dashed #216DAB"/&gt;&lt;/b&gt;&lt;/font&gt; &amp;nbsp; &lt;input type="image" src="images/search1.png" name="submit" value="Search" align="right"/&gt; &lt;/form&gt; &lt;hr size="20" noshade="noshade" color="#216DAB"&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; &lt;P&gt;&lt;font face="Arial" Size="6px" color="black" &gt;&lt;b&gt;List of files&lt;/b&gt;&lt;/font&gt;&lt;/p&gt; &lt;P&gt;&lt;center&gt;&lt;table border="0" bgcolor="#E0E0E0" style="border:1px dotted black"&gt;&lt;td&gt;&lt;?=$thelist?&gt;&lt;/td&gt;&lt;/table&gt;&lt;/center&gt;&lt;/p&gt; </code></pre> <p>However I get a PHP error when I load the php page.</p> <pre><code>Notice: Undefined variable: thelist in C:\xampp\htdocs\viewer\list.php on line 76 </code></pre> <p>TryCatch part:</p> <pre><code>while (false !== ($file = readdir($handle))) { if ($file != "." &amp;&amp; $file != "..") { if ( ($file = @scandir('./pdf/') &amp;&amp; (count($file) &gt; 0) ) ) { $directory_not_empty = TRUE; } { throw new Exception('No files in database'); } try { @$thelist .= '&lt;font face="Calibri" size="4"&gt;&lt;p&gt;&lt;b&gt;&lt;img src="images/document.png"&gt; '.$file.'&lt;br&gt;&lt;/b&gt;&lt;/p&gt;&lt;/a&gt;&lt;/font&gt;'; $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-&gt;getMessage); } } } closedir($handle); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload