From eef971e3ebe5fcba33e01d44bd489ff3a2312ab8 Mon Sep 17 00:00:00 2001 From: Tobias Reich Date: Sat, 29 Aug 2015 21:55:32 +0200 Subject: [PATCH] Catch upload errors #393 --- php/modules/Import.php | 1 + php/modules/Photo.php | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/php/modules/Import.php b/php/modules/Import.php index 381e9b1..1872bbe 100644 --- a/php/modules/Import.php +++ b/php/modules/Import.php @@ -42,6 +42,7 @@ class Import extends Module { $nameFile[0]['tmp_name'] = $path; $nameFile[0]['error'] = 0; $nameFile[0]['size'] = $size; + $nameFile[0]['error'] = UPLOAD_ERR_OK; if (!$photo->add($nameFile, $albumID, $description, $tags, true)) return false; return true; diff --git a/php/modules/Photo.php b/php/modules/Photo.php index 402c42f..27bc1ff 100755 --- a/php/modules/Photo.php +++ b/php/modules/Photo.php @@ -88,6 +88,41 @@ class Photo extends Module { foreach ($files as $file) { + # Check if file exceeds the upload_max_filesize directive + if ($file['error']===UPLOAD_ERR_INI_SIZE) { + Log::error($this->database, __METHOD__, __LINE__, 'The uploaded file exceeds the upload_max_filesize directive in php.ini'); + if ($returnOnError===true) return false; + exit('Error: The uploaded file exceeds the upload_max_filesize directive in php.ini!'); + } + + # Check if file was only partially uploaded + if ($file['error']===UPLOAD_ERR_PARTIAL) { + Log::error($this->database, __METHOD__, __LINE__, 'The uploaded file was only partially uploaded'); + if ($returnOnError===true) return false; + exit('Error: The uploaded file was only partially uploaded!'); + } + + # Check if writing file to disk failed + if ($file['error']===UPLOAD_ERR_CANT_WRITE) { + Log::error($this->database, __METHOD__, __LINE__, 'Failed to write photo to disk'); + if ($returnOnError===true) return false; + exit('Error: Failed to write photo to disk!'); + } + + # Check if a extension stopped the file upload + if ($file['error']===UPLOAD_ERR_EXTENSION) { + Log::error($this->database, __METHOD__, __LINE__, 'A PHP extension stopped the file upload'); + if ($returnOnError===true) return false; + exit('Error: A PHP extension stopped the file upload!'); + } + + # Check if the upload was successful + if ($file['error']!==UPLOAD_ERR_OK) { + Log::error($this->database, __METHOD__, __LINE__, 'Upload contains an error (' . $file['error'] . ')'); + if ($returnOnError===true) return false; + exit('Error: Upload failed!'); + } + # Verify extension $extension = getExtension($file['name']); if (!in_array(strtolower($extension), Photo::$validExtensions, true)) {