public function getSizeString() { $units = array('B', 'KB', 'MB', 'GB', 'TB'); $bytes = parent::getSize(); $bytes = max($bytes, 0); $pow = floor(($bytes ? log($bytes) : 0) / log(1024)); $pow = min($pow, count($units) - 1); $bytes /= pow(1024, $pow); return round($bytes, 2) . ' ' . $units[$pow]; }
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <?php $dir = new DirectoryIterator("E:/AppServ"); echo "<table border='1' width='500'>"; echo "<tr><th>目录/文件名</th><th>大小</th><th>操作</th></tr>"; while ($dir->valid()) { echo "<tr>"; $d = $dir->current(); if ($dir->isDir()) { echo "<td><a href=''>{$d}</a></td>"; echo "<td> </td>"; echo "<td> </td>"; } else { echo "<td>" . $d . "</td>"; echo "<td>" . $dir->getSize() . "byte</td>"; echo "<td><a href=''>删除</a></td>"; } echo "</tr>"; $dir->next(); } echo "</table>"; ?> </body> </html>
public function buildTableRow(DirectoryIterator $file, $includeParentDirectoryDots = true) { if (!$file->isDot() && substr($file->getFilename(), 0, 1) == '.' && Administration::instance()->Configuration->get('show-hidden', 'filemanager') != 'yes') { return; } elseif ($file->isDot() && !$includeParentDirectoryDots && $file->getFilename() == '..') { return; } elseif ($file->getFilename() == '.') { return; } $relpath = str_replace($this->getStartLocation() == '' ? DOCROOT : DOCROOT . $this->getStartLocation(), NULL, $file->getPathname()); if (!$file->isDir()) { //if(File::fileType($file->getFilename()) == self::CODE) // $download_uri = self::baseURL() . 'edit/?file=' . urlencode($relpath); //else $download_uri = self::baseURL() . 'download/?file=' . urlencode($relpath); } else { $download_uri = self::baseURL() . 'properties/?file=' . urlencode($relpath) . '/'; } if (!$file->isDot()) { $td1 = Widget::TableData(Widget::Anchor($file->getFilename(), self::baseURL() . ($file->isDir() ? 'browse' . $relpath . '/' : 'properties/?file=' . urlencode($relpath)), NULL, 'file-type ' . ($file->isDir() ? 'folder' : File::fileType($file->getFilename())))); //$group = (function_exists('posix_getgrgid') ? posix_getgrgid($file->getGroup()) : $file->getGroup()); //$owner = (function_exists('posix_getpwuid') ? posix_getpwuid($file->getOwner()) : $file->getOwner()); $group = $file->getGroup(); $owner = $file->getOwner(); $td3 = Widget::TableData(File::getOctalPermission($file->getPerms()) . ' <span class="inactive">' . File::getReadablePerm($file->getPerms()), NULL, NULL, NULL, array('title' => (isset($owner['name']) ? $owner['name'] : $owner) . ', ' . (isset($group['name']) ? $group['name'] : $group)) . '</span>'); $td4 = Widget::TableData(DateTimeObj::get(__SYM_DATETIME_FORMAT__, $file->getMTime())); if ($file->isWritable()) { if ($file->isDir()) { $td5 = Widget::TableData(Widget::Anchor('Edit', $download_uri)); } else { $td5 = Widget::TableData(Widget::Anchor('Download', $download_uri)); } } else { $td5 = Widget::TableData('-', 'inactive'); } } else { $td1 = Widget::TableData(Widget::Anchor('↵', self::baseURL() . 'browse' . $relpath . '/')); $td3 = Widget::TableData('-', 'inactive'); $td4 = Widget::TableData('-', 'inactive'); $td5 = Widget::TableData('-', 'inactive'); } $td2 = Widget::TableData($file->isDir() ? '-' : General::formatFilesize($file->getSize()), $file->isDir() ? 'inactive' : NULL); $startlocation = DOCROOT . $this->getStartLocation(); if (!$file->isDot()) { $td5->appendChild(Widget::Input('items[' . str_replace($startlocation, '', $file->getPathname()) . ($file->isDir() ? '/' : NULL) . ']', NULL, 'checkbox')); } return Widget::TableRow(array($td1, $td2, $td3, $td4, $td5)); }
/** * Process file * * @param int $directoryID * @param DirectoryIterator $entries */ protected final function _processFile($directoryID, DirectoryIterator $entries) { $scanID = $this->_scanRow->scanID; // Load row when exists $fileRow = $this->_fileTable->fetchRow(array('directoryID' => $directoryID, 'name' => $entries->getFilename())); // Already scanned if ($fileRow && $fileRow->lastScanID == $scanID) { return; } $pathname = $entries->getPathname(); // Data $data = array('modifyDate' => $this->_normalizeDate($entries->getMTime()), 'owner' => $this->_normalizeUser($entries->getOwner()), 'group' => $this->_normalizeGroup($entries->getGroup()), 'permissions' => $entries->getPerms(), 'size' => $entries->getSize(), 'linkTarget' => $entries->isLink() ? $entries->getLinkTarget() : null); // Content $contentHash = null; if (!$entries->isLink() && ($this->_alwaysCheckContent || !$fileRow || $fileRow->modifyDate != $data['modifyDate'] || $fileRow->size != $data['size'])) { // Non-accessible file if (!is_readable($pathname)) { $this->_log(self::LOG_ERROR, "\t{$pathname} cannot be read."); if ($fileRow) { $contentHash = $fileRow->contentHash; } } else { $contentHash = md5_file($pathname); } } // Transaction $this->_db->beginTransaction(); // New row if ($newRow = !$fileRow) { fwrite(STDOUT, "\t{$pathname} is new.\n"); $fileRow = $this->_createFileRow(array('directoryID' => $directoryID, 'name' => $entries->getFilename()) + $data); } // Store values $oldValues = $fileRow->toArray(); // Content if ($fileRow->contentHash != $contentHash) { $data['contentHash'] = $contentHash; if ($this->_storagePath) { $data['storedAs'] = $this->_copyFile($fileRow->fileID, $entries); } } // Update row $this->_updateFileRow($pathname, $fileRow, $data); $fileRow->lastScanID = $scanID; $fileRow->save(); // Scan row update $this->_scanRow->lastOperationDate = new SeekR_Expression('NOW()'); $this->_scanRow->save(); // Transaction $this->_db->commit(); }
private function _filestackProcessDir($dir) { $Iterator = new \DirectoryIterator($dir); if ($this->_filestackKey > 0) { $Iterator->seek($this->_filestackKey); } while ($Iterator->valid()) { // timeout check: if ($this->intervalLimitIsReached() === true) { $this->_Dirstack->save(); $this->_Filestack->save(); $this->setMessage('Filestack creation: ' . $this->_filesInStack . ' files processed.'); return false; } $this->_filestackKey = $Iterator->key(); $this->Storage->set('filestackKey', $this->_filestackKey, 'filecheck'); // skip dots: if ($Iterator->isDot()) { $Iterator->next(); continue; } // if is directory save to database for later processing: $currentPath = $Iterator->getPathname(); if ($Iterator->isDir()) { $this->_Dirstack->addPath($currentPath); $Iterator->next(); continue; } // filesize check: $filesize = $Iterator->getSize(); if (empty($filesize) || $filesize > $this->sizeFilter) { $Iterator->next(); continue; } // filetype check: if ($this->_extensionCheck === true) { $fileExtension = $Iterator->getExtension(); if (empty($fileExtension) || !isset($this->_allowedExtensions[$fileExtension])) { $Iterator->next(); continue; } } // add file to stack: $this->_Filestack->addPath($currentPath); // create hash (if not yet done): if ($this->_createFilehashDb === true) { $pathhash = sha1($currentPath); $filehash = sha1_file($currentPath); $this->Database->setQuery("INSERT IGNORE INTO #__wm_filehashes (pathhash, filehash, mode) VALUES(" . $this->Database->q($pathhash) . "," . $this->Database->q($filehash) . ", " . $this->Database->q($this->runMode) . ")")->execute(); } $this->_filesInStack++; $this->Storage->set('filesInStack', $this->_filesInStack, 'filecheck'); $Iterator->next(); } unset($Iterator); // current dir completed -> delete from stack: $this->_Dirstack->save(); $this->_Dirstack->clear(); $this->_Filestack->save(); $this->_Filestack->clear(); $this->Database->setQuery("DELETE FROM #__wm_dirstack WHERE path = " . $this->Database->q($dir) . "AND mode = " . $this->Database->q($this->runMode))->execute(); $this->_filestackKey = 0; // select next dir from stack: $this->Database->setQuery("SELECT path FROM #__wm_dirstack WHERE mode = " . $this->Database->q($this->runMode) . " LIMIT 1")->execute(); $row = $this->Database->getRow(); if (empty($row)) { return true; } $this->_filestackPath = $row->path; $this->Storage->set('filestackPath', $this->_filestackPath, 'filecheck'); return $this->_filestackProcessDir($row->path); }
<?php /* create directory iterator based on current directory */ $dir = new DirectoryIterator(dirname(__FILE__)); while ($dir->valid()) { // while there are valid entries if ($dir->isFile()) { // check if we are dealing with a file /* print file name & it's size */ echo "Name: " . $dir->getFilename() . ", size: " . $dir->getSize() . "<br />"; } $dir->next(); // move to next entry }
/** * Determine file/directory statistics for input item. * * @param DirectoryIterator $item Item passed from a DirectoryIterator * @return array Listing with stats */ private static function _getItemStats(DirectoryIterator $item) { $list = array(); // add filename $list['name'] = (string) $item; // add directory/file type $list['type'] = $item->getType(); // add modification time $list['modified'] = date('Y-m-d H:i:s', $item->getMTime()); // get permissions $list['permissions'] = $item->getPerms(); // is writable? $list['writable'] = $item->isWritable() ? 1 : 0; // add path $list['path'] = $item->getPathName(); $list['real_path'] = $item->getRealPath(); // add size $list['size'] = Fari_Format::bytes($item->getSize()); return $list; }