From b6b1d1cac1e804adbe19175e0e276cd566f9f447 Mon Sep 17 00:00:00 2001 From: skodak Date: Wed, 26 Sep 2007 16:53:26 +0000 Subject: [PATCH] MDL-11460 new function moodleform->get_file_content($elname) in case we need to get content of uploaded files --- lib/formslib.php | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/lib/formslib.php b/lib/formslib.php index 0431b60003..b0a5840a51 100644 --- a/lib/formslib.php +++ b/lib/formslib.php @@ -419,6 +419,38 @@ class moodleform { return $this->_upload_manager->get_new_filename(); } + /** + * Get content of uploaded file. + * @param $element name of file upload element + * @return mixed false in case of failure, string if ok + */ + function get_file_content($elname) { + if (!$this->is_submitted() or !$this->is_validated()) { + return false; + } + + if (!$this->_form->elementExists($elname)) { + return false; + } + + if (empty($this->_upload_manager->files[$elname]['tmp_name'])) { + return false; + + } else { + $data = ""; + $file = @fopen($this->_upload_manager->files[$elname]['tmp_name'], "rb"); + if ($file) { + while (!feof($file)) { + $data .= fread($file, 1024); // TODO: do we really have to do this? + } + fclose($file); + return $data; + } else { + return false; + } + } + } + /** * Print html form. */ -- 2.39.5