1: <?php
2: /**
3: * ManiaLib - Lightweight PHP framework for Manialinks
4: *
5: * @see http://code.google.com/p/manialib/
6: * @copyright Copyright (c) 2009-2011 NADEO (http://www.nadeo.com)
7: * @license http://www.gnu.org/licenses/lgpl.html LGPL License 3
8: * @version $Revision: 667 $:
9: * @author $Author: melot.philippe $:
10: * @date $Date: 2012-02-23 15:44:10 +0100 (jeu., 23 févr. 2012) $:
11: */
12:
13: namespace ManiaLib\Utils;
14:
15: final class Upload
16: {
17:
18: const UPLOADED_FILE_RIGHTS = 0770;
19:
20: /**
21: * Tries to read the specified file and save it
22: * @param string The path where the file will be saved
23: * @param string filename
24: * @param int The maximum file size in bytes
25: */
26: static function uploadFile($path, $filename, $maxSize = 2097152)
27: {
28: $inputFile = file_get_contents('php://input', null, null, null, $maxSize);
29:
30: // Else try to get GET data
31: if($inputFile === false && array_key_exists('input', $_GET))
32: {
33: $inputFile = $_GET['input'];
34: }
35:
36: // Check for error
37: if($inputFile === false)
38: {
39: throw new \Exception('Couldn\'t read input file');
40: }
41: if(!file_put_contents($path.$filename, $inputFile))
42: {
43: throw new \Exception('Couldn\'t save input file to '.$path.$filename);
44: }
45: if(filesize($path.$filename) > $maxSize)
46: {
47: // Not sure if usefull here
48: unlink($path.$filename);
49: throw new FileTooLargeException();
50: }
51: }
52: }
53:
54: class FileTooLargeException extends \ManiaLib\Application\UserException
55: {
56:
57: }
58:
59: ?>