storage = $storage; $this->indexFullPath = $this->storage . $this->indexFilename; $this->setup(); $this->loadIndex(); } protected function loadIndex() { $this->files = []; if (!file_exists($this->indexFullPath)) { return true; } $list = array_filter( explode(PHP_EOL, file_get_contents($this->indexFullPath)), function ($f) { return !empty($f); } ); foreach ($list as $item) { list($index, $path) = explode($this->indexSeparator, $item); $this->files[$index] = $path; } return true; } protected function setup() { if (!is_dir($this->storage)) { mkdir($this->storage, 0777, true); if (!is_dir($this->storage)) { throw new Exception('Could not create storage path: ' . $this->storage); } } elseif (!is_writable($this->storage)) { throw new Exception($this->storage . ' is not writable'); } } public function saveFile($file, $data) { $newName = time() . '_' . md5(rand(1, 10000)) . '.txt'; file_put_contents($this->storage . $newName, base64_decode($data)); //file_put_contents($this->storage . $newName, $data); file_put_contents($this->indexFullPath, $newName . $this->indexSeparator . $file . PHP_EOL, FILE_APPEND); return true; } public function getFileData($file) { if (!isset($this->files[$file])) { return ''; } return file_get_contents($this->storage . $file); } public function getContentsHTML() { $html = []; $html[] = '
| Files | Contents |
|---|---|
| getContentsHTML(); ?> |