我有一个遍历目录层次结构的文件。子目录由员工的 ID 命名。我已经实例化了员工类以获取员工的姓名,以便我可以输出姓名而不是 ID。
我不断收到错误“致命错误:调用非对象上的成员函数,我不明白为什么。
我可以使用 var_dump() 转储对象并查看所有内容。只有 3 个子目录,每个子目录包含 4 个文件。
下面是我的代码:
<div id="empDir1" style="padding-left:20px; padding-bottom:25px;">
<?php
$dirPath = "emp_files";
function fileManager( $dir ) {
$dirParts = explode("/", $dir);
$dirId = $dirParts[1];
if ( !isset( $dirId ) ){
echo "<div></div>";
} else {
$employeeId = $dirId;
$employee = Employee::getEmployee( $employeeId );
$emp_lastname = $employee->getValueEncoded('emp_lastname');
$emp_firstname = $employee->getValueEncoded('emp_firstname');
$dirListing = $emp_lastname . ', ' . $emp_firstname;
}
echo "<h2>File Listing for $dirListing ...</h2>";
if ( !( $handle = opendir( $dir ) ) ) die( "Cannot open $dir." );
$fileImage = array(
'doc' => '../../images/folder-doc.png',
'txt' => '../../images/folder-txt.png',
'msg' => '../../images/folder-msg.png',
'pdf' => '../../images/folder-pdf.png',
'png' => '../../images/folder-png.png',
'jpg' => '../../images/folder-jpg.png',
'bmp' => '../../images/folder-bmp.png',
'gif' => '../../images/folder-gif.png',
'xls' => '../../images/folder-xls.png',
);
$files = array();
while ( $file = readdir( $handle ) ) {
if ( $file != "." && $file != ".." ) {
if ( is_dir( $dir . "/" . $file ) ) $file .= "/";
$files[] = $file;
}
}
sort( $files );
echo '<table>';
foreach ( $files as $file ) {
$fileParts = explode('.', $file);
$fileExt = strtolower($fileParts[count($fileParts) -1]);
echo '<tr>';
echo '<td width="650px" class="row2">';
echo ("<a class='ptext' href=" . $dir . "/" . $file . ">" . ( isset($fileImage[$fileExt] ) ? "<img src='". $fileImage[$fileExt] . "' />" : "<img src='../../images/folder-unknown.png' />" ) . " $file</a><a style='float:right;padding-right:10px;' href='empUnlink.php?filePath=" . $dir . $file ."' id='" . $dir . $file ."' class='deleteFile'>x</a></p>");
echo '</td>';
echo '</tr>';
}
echo '</table>';
foreach ( $files as $file ) {
if ( substr( $file, -1 ) == "/" ) fileManager( "$dir/" . substr( $file, 0, -1 ) );
}
closedir( $handle );
}
fileManager( $dirPath );
?>