Skip to content

Instantly share code, notes, and snippets.

@yus-ham
Last active August 5, 2019 04:33
Show Gist options
  • Save yus-ham/032f768b90be072ea6e9d03b12d02f19 to your computer and use it in GitHub Desktop.
Save yus-ham/032f768b90be072ea6e9d03b12d02f19 to your computer and use it in GitHub Desktop.
File Manager
<?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 ? '[+]' : '(&middot;)';
return $icon ." <a href='" . FileManager::homeUrl() . $file->getPathname() ."'>". $file->getBasename(). "</a><hr>\n";
}
}
<?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);
}
}
<?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'];
}
}
<?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;
}
}
}
@yus-ham
Copy link
Author

yus-ham commented Aug 5, 2019

integrasi Flysystem ?????

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment