Last active
August 5, 2019 04:33
-
-
Save yus-ham/032f768b90be072ea6e9d03b12d02f19 to your computer and use it in GitHub Desktop.
File Manager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace fm; | |
class Directory extends File | |
{ | |
public function open() { | |
echo "<h2>Index of: ". $this->path ."</h2>\n"; | |
$files = new \DirectoryIterator($this->path); | |
foreach ($files as $file) { | |
$is_dir = $file->isDir(); | |
$line = $this->renderLine($file, $is_dir); | |
$is_dir ? $dirs["$file"] = $line : $_files["$file"] = $line; | |
} | |
isset($dirs) && ksort($dirs); | |
isset($_files) && ksort($_files); | |
print implode('', (array)@$dirs); | |
print implode('', (array)@$_files); | |
} | |
protected function renderLine($file, $is_dir) { | |
$icon = $is_dir ? '[+]' : '(·)'; | |
return $icon ." <a href='" . FileManager::homeUrl() . $file->getPathname() ."'>". $file->getBasename(). "</a><hr>\n"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace fm; | |
class File | |
{ | |
private $path; | |
private $isReadable; | |
public function __construct($path) { | |
$this->path = $path; | |
} | |
public function __get($prop) { | |
return $this->$prop; | |
} | |
public function open() { | |
echo $this->path; | |
return; | |
header('Content-Description: File Transfer'); | |
header('Content-Type: application/octet-stream'); | |
header('Content-Disposition: attachment; filename="' . basename($this->path) . '"'); | |
header('Expires: 0'); | |
header('Cache-Control: must-revalidate'); | |
header('Pragma: public'); | |
header('Content-Length: ' . filesize($this->path)); | |
readfile($this->path); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace fm; | |
class FileManager | |
{ | |
public static function run() { | |
$path = self::getUri(); | |
$file = is_dir($path) ? 'fm\Directory' : 'fm\File'; | |
$file = new $file($path); | |
$file->open(); | |
} | |
public static function getUri() { | |
return $_SERVER ['PATH_INFO'] ?: dirname($_SERVER['SCRIPT_FILENAME']); | |
} | |
public static function homeUrl() { | |
return $_SERVER['SCRIPT_NAME']; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
spl_autoload_register('autoloader'); | |
fm\FileManager::run(); | |
function autoloader($class) { | |
$paths = [ | |
'fm' => '.', | |
]; | |
foreach ($paths as $ns => $path) { | |
foreach ((array)$path as $p) { | |
$p = trim($p, '/'); | |
$file = __DIR__.($p?'/'.$p:null).'/'. substr($class, strlen($ns)); | |
$file = str_replace('\\', '/', $file) .'.php'; | |
if(is_file($file)) { | |
include $file; | |
} | |
} | |
if (strpos($class, $ns.'\\') === 0) { | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
integrasi Flysystem ?????