From f2968d08a9d7e9d4277a2ccdea73ddbbf12ed570 Mon Sep 17 00:00:00 2001 From: Petr Skoda Date: Tue, 1 Jun 2010 08:20:01 +0000 Subject: [PATCH] MDL-22634 fixed exception when adding files with the same name via file browser - more methods --- lib/file/file_info_stored.php | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/file/file_info_stored.php b/lib/file/file_info_stored.php index cd062542c75..830164e4da6 100644 --- a/lib/file/file_info_stored.php +++ b/lib/file/file_info_stored.php @@ -304,6 +304,8 @@ class file_info_stored extends file_info { return null; } + $fs = get_file_storage(); + $now = time(); $newrecord = new object(); @@ -313,13 +315,16 @@ class file_info_stored extends file_info { $newrecord->filepath = $this->lf->get_filepath(); $newrecord->filename = $newfilename; + if ($fs->file_exists($newrecord->contextid, $newrecord->filearea, $newrecord->itemid, $newrecord->filepath, $newrecord->filename)) { + // file already exists, sorry + return null; + } + $newrecord->timecreated = $now; $newrecord->timemodified = $now; $newrecord->mimetype = mimeinfo('type', $newfilename); $newrecord->userid = $userid; - $fs = get_file_storage(); - if ($file = $fs->create_file_from_string($newrecord, $content)) { return $this->browser->get_file_info($this->context, $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $file->get_filename()); } @@ -389,6 +394,8 @@ class file_info_stored extends file_info { return null; } + $fs = get_file_storage(); + $now = time(); $newrecord = new object(); @@ -398,13 +405,16 @@ class file_info_stored extends file_info { $newrecord->filepath = $this->lf->get_filepath(); $newrecord->filename = $newfilename; + if ($fs->file_exists($newrecord->contextid, $newrecord->filearea, $newrecord->itemid, $newrecord->filepath, $newrecord->filename)) { + // file already exists, sorry + return null; + } + $newrecord->timecreated = $now; $newrecord->timemodified = $now; $newrecord->mimetype = mimeinfo('type', $newfilename); $newrecord->userid = $userid; - $fs = get_file_storage(); - if ($file = $fs->create_file_from_storedfile($newrecord, $fid)) { return $this->browser->get_file_info($this->context, $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $file->get_filename()); }