-1

我有一个遍历目录层次结构的文件。子目录由员工的 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 );
              ?>
4

1 回答 1

1

好吧,在不知道 class 的代码是什么的情况下Employee,我的猜测是静态函数Employee::getEmployee()没有找到与 匹配的员工$employeeid,并返回 null。$employee在尝试使用它之前做一个不为空的检查。

于 2012-10-29T20:56:27.880 回答