MDL-14589 improved file handling operations & towards zipping support

This commit is contained in:
skodak 2008-08-02 12:45:02 +00:00
parent 7c61a8f047
commit 6c0e2d08a9
6 changed files with 51 additions and 46 deletions

View File

@ -7,7 +7,7 @@
$courseid = optional_param('id', 0, PARAM_INT);
$contextid = optional_param('contextid', SYSCONTEXTID, PARAM_INT);
$filearea = optional_param('filearea', '', PARAM_SAFEDIR);
$filearea = optional_param('filearea', '', PARAM_ALPHAEXT);
$itemid = optional_param('itemid', -1, PARAM_INT);
$filepath = optional_param('filepath', '', PARAM_PATH);
$filename = optional_param('filename', '', PARAM_FILE);

View File

@ -23,10 +23,8 @@ class file_storage {
throw new file_exception('localfilecannotcreatefiledirs'); // permission trouble
}
// place warning file in file pool root
$fp = fopen($this->filedir.'/warning.txt', 'w');
fwrite($fp, 'This directory contains the content of uploaded files and is controlled by Moodle code. Do not manually move, change or rename any of the files and subdirectories here.');
fclose($fp);
unset($fp);
file_put_contents($this->filedir.'/warning.txt',
'This directory contains the content of uploaded files and is controlled by Moodle code. Do not manually move, change or rename any of the files and subdirectories here.');
}
}
@ -199,7 +197,7 @@ class file_storage {
throw new file_exception('localfileproblem', 'Invalid contextid');
}
$filearea = clean_param($filearea, PARAM_SAFEDIR);
$filearea = clean_param($filearea, PARAM_ALPHAEXT);
if ($filearea === '') {
throw new file_exception('localfileproblem', 'Invalid filearea');
}
@ -289,7 +287,7 @@ class file_storage {
}
if ($key == 'filearea') {
$value = clean_param($value, PARAM_SAFEDIR);
$value = clean_param($value, PARAM_ALPHAEXT);
if ($value === '') {
throw new file_exception('localfileproblem', 'Invalid filearea');
}
@ -353,7 +351,7 @@ class file_storage {
throw new file_exception('localfileproblem', 'Invalid contextid');
}
$file_record->filearea = clean_param($file_record->filearea, PARAM_SAFEDIR);
$file_record->filearea = clean_param($file_record->filearea, PARAM_ALPHAEXT);
if ($file_record->filearea === '') {
throw new file_exception('localfileproblem', 'Invalid filearea');
}
@ -428,7 +426,7 @@ class file_storage {
throw new file_exception('localfileproblem', 'Invalid contextid');
}
$file_record->filearea = clean_param($file_record->filearea, PARAM_SAFEDIR);
$file_record->filearea = clean_param($file_record->filearea, PARAM_ALPHAEXT);
if ($file_record->filearea === '') {
throw new file_exception('localfileproblem', 'Invalid filearea');
}
@ -519,17 +517,9 @@ class file_storage {
}
$newfile = true;
$fs = fopen($pathname, 'rb');
$fp = fopen($hashfile, 'wb');
while(!feof($fs)) {
$buf = fread($fs, 65536);
if ($buf === false) {
throw new file_exception('localfilecannotread');
}
fwrite($fp, $buf);
if (!copy($pathname, $hashfile)) {
throw new file_exception('localfilecannotread');
}
fclose($fp);
fclose($fs);
if (filesize($hashfile) !== $filesize) {
@unlink($hashfile);
@ -566,10 +556,7 @@ class file_storage {
}
$newfile = true;
$fp = fopen($hashfile, 'wb');
fwrite($fp, $content);
fclose($fp);
file_put_contents($hashfile, $content);
if (filesize($hashfile) !== $filesize) {
@unlink($hashfile);

View File

@ -36,7 +36,7 @@ class stored_file {
}
/**
* Protected - devs must not gain direct access to this function
* Protected - developers must not gain direct access to this function
**/
protected function get_content_file_location() {
// NOTE: do not make this public, we must not modify or delete the pool files directly! ;-)
@ -80,6 +80,19 @@ class stored_file {
return file_get_contents($this->get_content_file_location());
}
/**
* Copy content of file to give npathname
* @param string $pathnema rela path to new file
* @return bool success
*/
public function copy_content_to($pathname) {
$path = $this->get_content_file_location();
if (!is_readable($path)) {
throw new file_exception('localfilecannotread');
}
return copy($path, $pathname);
}
public function get_contextid() {
return $this->file_record->contextid;
}
@ -119,4 +132,8 @@ class stored_file {
public function get_timemodified() {
return $this->file_record->timemodified;
}
public function get_status() {
return $this->file_record->status;
}
}

View File

@ -5,6 +5,7 @@ define('BYTESERVING_BOUNDARY', 's1k2o3d4a5k6s7'); //unique string constant
require_once("$CFG->libdir/file/file_exceptions.php");
require_once("$CFG->libdir/file/file_storage.php");
require_once("$CFG->libdir/file/file_browser.php");
require_once("$CFG->libdir/file/file_packer.php");
function get_file_url($path, $options=null, $type='coursefile') {
global $CFG;

View File

@ -530,19 +530,9 @@ class moodleform {
return false;
}
}
if (!$temp = @fopen($_FILES[$elname]['tmp_name'], "rb")) {
if (!copy($_FILES[$elname]['tmp_name'], $pathname)) {
return false;
}
if (!$file = @fopen($pathname, "wb")) {
return false;
}
while (!feof($temp)) {
$data = fread($temp, 65536);
fwrite($file, $data);
}
fclose($file);
fclose($temp);
return true;
}
@ -604,17 +594,7 @@ class moodleform {
return false;
}
if (!$file = @fopen($_FILES[$elname]['tmp_name'], "rb")) {
return false;
}
$data = '';
while (!feof($file)) {
$data .= fread($file, 4048);
}
fclose($file);
return $data;
return file_get_contents($_FILES[$elname]['tmp_name']);
}
/**

View File

@ -4504,6 +4504,26 @@ function get_file_browser() {
return $fb;
}
/**
* Returns local file storage instance
* @return object file_storage
*/
function get_file_packer() {
global $CFG;
static $fp = null;
if ($fp) {
return $fp;
}
require_once("$CFG->libdir/filelib.php");
$fp = new file_packer();
return $fp;
}
/**
* Makes an upload directory for a particular module.
*