I need to write the class in PHP. Input takes the path (directory name). The class has two methods:
- files - returns an array of file names of this directory.
- table - re开发者_开发百科turns a generated html table with a list of files in this directory.
How do I do this?
You don't need to create a class for this. In fact, you should not. That is overly complex and entirely unnecessary.
If you need to read the list of files in a directory, you have three options: readdir
, glob
, or DirectoryIterator.
If you need to read the list of files in a directory, and all the directories inside of it, then you want to use RecursiveDirectoryIterator.
There are adequate usage examples on the linked documentation pages.
Using these, you can get your list'o'files and build your HTML.
I think it is good to create a class for such functionaliteit, because you can much better predict its behaviour and work around subtle changes in PHP over different versions. Also, you can create different classes to read lists of files from other locations like a database or csv file.
In this case, I extended a renderer class from the base class, because I figured the rendering shouldn't be part of the base class. In fact, it might be better to create a separate class for this that uses any descendant of Folder to render it. But I'll leave it to you to figure out what suits you best.
<?
class Folder
{
private $folder = '';
public function getFolder()
{
return $this->folder;
}
protected function setFolder($value)
{
$this->folder = (string)$folder;
}
public __construct($folder)
{
this->setFolder($folder);
}
public function readFileNames()
{
$folder = (string)$this->getFolder();
if ($folder === '')
{
throw new exception("Folder name is empty");
}
if (is_dir($folder) !== true)
{
throw new exception("'$folder' is not a directory.");
}
$dir = @opendir($this->folder);
if ($dir === false)
{
throw new exception("Cannot open directory '$folder'.");
}
$result = array();
while (false !== ($fileName = readdir($dir)))
{
if (is_file($fileName) === true)
{
$result[] = $fileName;
}
}
return $result;
}
public function readFiles()
{
$fileNames = $this->readFileNames();
$fileNames = array_flip($fileNames);
foreach($fileNames as $fileName=>&$fileContents)
{
if (false === ($file = @readFile($fileName)))
{
$fileContents = null;
}
else
{
$fileContents = $file;
}
}
}
}
class FolderTableRenderer extends Folder
{
public function renderTable()
{
?>
<table>
<thead><tr><th>File name</th></tr></thead>
<tbody>
<?
foreach ($this->readFileNames() as $fileName)
{
?>
<tr><td><?=$fileName?></td></tr>
<?
}
?>
</tbody>
</table>
<?
}
}
Very simple example of class.
class MyFiles {
public static function files($path) {
// Logic used to get the files found at path and return an array
// Just use the built-in functionality of PHP
$filesArray = array();
if (!is_dir($path)) {
return $filesArray;
}
$dir = opendir($path);
while (false !== ($filename = readdir($dir))) {
if (!is_dir($filename)) {
$filesArray[] = $filename;
}
}
return $filesArray;
}
public static function table($path) {
$files = self::files($path);
$c_files = count($files);
if ($c_files == 0) {
return "<table><tbody><tr><td>No files at $path</td></tr></tbody></table>";
}
$table = "";
for ($i = 0; $i < $c_files; $i++) {
$table .= "<tr><td>{$files[$i]}</td></tr>";
}
return "<table><tbody>$table</tbody></table>";
}
}
$fileTable = MyFiles::table('/my/path/with/files');
精彩评论