diff --git a/phpBB/language/en/common.php b/phpBB/language/en/common.php index 006582772a..19a9a720cb 100644 --- a/phpBB/language/en/common.php +++ b/phpBB/language/en/common.php @@ -736,6 +736,8 @@ $lang = array_merge($lang, array( 'STORAGE_CANNOT_RENAME' => 'Can not rename file or folder.', 'STORAGE_CANNOT_COPY' => 'Can not copy file or folder.', 'STORAGE_CANNOT_CREATE_DIR' => 'Can not create directory.', + 'STORAGE_CANNOT_OPEN_FILE' => 'Can not open file.', + 'STORAGE_CANNOT_CREATE_FILE' => 'Can not create file.', 'TB' => 'TB', 'TERMS_USE' => 'Terms of use', diff --git a/phpBB/phpbb/storage/adapter/adapter_interface.php b/phpBB/phpbb/storage/adapter/adapter_interface.php index 207f001fad..1a4fe5c046 100644 --- a/phpBB/phpbb/storage/adapter/adapter_interface.php +++ b/phpBB/phpbb/storage/adapter/adapter_interface.php @@ -84,4 +84,26 @@ interface adapter_interface * When the file cannot be copied */ public function copy($path_orig, $path_dest); + + /** + * Reads a file as a stream. + * + * @param string $path File to read + * + * @throws \phpbb\storage\exception\exception When cannot open file + * + * @return resource Returns a file pointer + */ + public function read_stream($path); + + /** + * Writes a new file using a stream. + * + * @param string $path The target file + * @param resource $resource The resource + * + * @throws \phpbb\storage\exception\exception When target file exists + * When target file cannot be created + */ + public function write_stream($path, $resource); } diff --git a/phpBB/phpbb/storage/adapter/local.php b/phpBB/phpbb/storage/adapter/local.php index a8498d6d8c..4c822d3a5e 100644 --- a/phpBB/phpbb/storage/adapter/local.php +++ b/phpBB/phpbb/storage/adapter/local.php @@ -195,4 +195,39 @@ class local implements adapter_interface $this->create_dir($path); } } + + /** + * {@inheritdoc} + */ + public function read_stream($path) + { + $stream = @fopen($path, 'rb'); + + if (!$stream) + { + throw new exception('STORAGE_CANNOT_OPEN_FILE', $path); + } + + return $stream; + } + + /** + * {@inheritdoc} + */ + public function write_stream($path, $resource) + { + if ($this->exists($path)) + { + throw new exception('STORAGE_FILE_EXISTS', $path); + } + + $stream = @fopen($path, 'w+b'); + + if (!$stream) + { + throw new exception('STORAGE_CANNOT_CREATE_FILE', $path); + } + + stream_copy_to_stream($resource, $stream); + } } diff --git a/phpBB/phpbb/storage/storage.php b/phpBB/phpbb/storage/storage.php index 65a4f6eb72..ff8f49c2e4 100644 --- a/phpBB/phpbb/storage/storage.php +++ b/phpBB/phpbb/storage/storage.php @@ -140,4 +140,32 @@ class storage { $this->get_adapter()->copy($path_orig, $path_dest); } + + /** + * Reads a file as a stream. + * + * @param string $path File to read + * + * @throws \phpbb\storage\exception\exception When cannot open file + * + * @return resource Returns a file pointer + */ + public function read_stream($path) + { + $this->get_adapter()->read_stream($path); + } + + /** + * Writes a new file using a stream. + * + * @param string $path The target file + * @param resource $resource The resource + * + * @throws \phpbb\storage\exception\exception When target file exists + * When target file cannot be created + */ + public function write_stream($path, $resource) + { + $this->get_adapter()->write_stream($path, $resource); + } }