PHP readdir(): 3 is not a valid Directory resource
<pre><code>    &lt;?php

function convert(){

 //enable error reporting for debugging
 error_reporting(E_ALL | E_STRICT);

 //convert pdf's to html using payroll.sh script and
 //pdftohtml command line tool
 $program = "payroll.sh";
 $toexec="sh /var/www/html/tmp/" . $program . " 2&gt;&amp;1";
 $exec=shell_exec($toexec);

 //display message from payroll.sh
 //echo $exec;
 //echo ('&lt;br/&gt;');

}

function process(){

$dir = '/var/www/html/tmp/converted';
//echo ('one');

if (is_dir($dir)) {
//echo ('two');
    if ($dh = opendir($dir)) {
 //echo ('three');
        while (($file = readdir($dh)) !== false) {
  //echo ('four');

   if ($file != "." &amp;&amp; $file != ".."){
   echo 'opening file: ';
   echo $file;
   echo ("&lt;br/&gt;");
   $fp = fopen('/var/www/html/tmp/converted/' . $file, 'r+');
   $count = 0;

      //while file is not at the EOF marker
      while (!feof($fp)) 
       {
        $line = fgets($fp);

        if($count==21)
         {
         $employeeID = substr($line,71,4);
         echo 'employee ID: ';
         echo $employeeID;
         echo ('&lt;br/&gt;');
         //echo ('six');
         $count++;
         }
        else if($count==30)
         {
         $employeeDate = substr($line,71,10);
         echo 'employee Date: ';
         echo $employeeDate;
         echo ('&lt;br/&gt;');
         //echo ('seven');
         $count++;
         }
        else 
         {
         //echo ('eight');
         //echo ('&lt;br/&gt;');
         $count++;
         }
         }
   fclose($fp);
   closedir($dh);

}
}
}
}
}

convert();
process();
?&gt;
</code></pre>

<p>I am setting up a php script that will take a paystub in pdf format, convert it to html, then import it into Drupal after getting the date and employee ID. </p>

<p>The code only seems to process the first file in the directory then it gives me this: </p>

<p><pre><code>opening file: dd00000112_28_2010142011-1.html
employee ID: 9871
employee Date: 12/31/2010</p>

<p>Warning: readdir(): 3 is not a valid Directory resource in /var/www/html/pay.mistequaygroup.com/payroll.php on line 29
</pre></code></p>

<p>The '3' in the error really confuses me, and google is not helping much. Could it be the 3rd iteration of the loop? The only files in the directory reddir() is scanning are the .html files waiting to be processed. Any ideas?</p>

<p>Also, how does my code look? I'm fairly new to doing any real programming and I don't get too much input around work.</p>
