<?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>&1";
$exec=shell_exec($toexec);
//display message from payroll.sh
//echo $exec;
//echo ('<br/>');
}
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 != "." && $file != ".."){
echo 'opening file: ';
echo $file;
echo ("<br/>");
$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 ('<br/>');
//echo ('six');
$count++;
}
else if($count==30)
{
$employeeDate = substr($line,71,10);
echo 'employee Date: ';
echo $employeeDate;
echo ('<br/>');
//echo ('seven');
$count++;
}
else
{
//echo ('eight');
//echo ('<br/>');
$count++;
}
}
fclose($fp);
closedir($dh);
}
}
}
}
}
convert();
process();
?>
Run Code Online (Sandbox Code Playgroud)
我正在设置一个PHP脚本,它将采用pdf格式的paystub,将其转换为html,然后在获取日期和员工ID后将其导入Drupal.
代码似乎只处理目录中的第一个文件然后它给我这个:
opening file: dd00000112_28_2010142011-1.html
employee ID: 9871
employee Date: 12/31/2010
Warning: readdir(): 3 is not a valid Directory resource in /var/www/html/pay.mistequaygroup.com/payroll.php on line 29
错误中的'3'确实让我感到困惑,谷歌并没有多大帮助.它可能是循环的第三次迭代吗?reddir()目录中唯一正在扫描的文件是等待处理的.html文件.有任何想法吗?
另外,我的代码看起来如何?我做任何真正的编程都是新手,我不会在工作中得到太多的输入.
在完成目录之前,您似乎正在关闭目录.移动这一行:
closedir($dh);
Run Code Online (Sandbox Code Playgroud)
在你的while循环之外:
while (($file = readdir($dh)) !== false) {
....
}
closedir($dh);
Run Code Online (Sandbox Code Playgroud)
而不是这个:
while (($file = readdir($dh)) !== false) {
....
closedir($dh);
}
Run Code Online (Sandbox Code Playgroud)