1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
 
// ---------------------------------------------------
// Made By: Nick George, 2008
// Last Updated: June 9, 2008 11:12 AM
// Filename: Uploader.php
// Description: Portable class allowing you to upload
//                            a file to a specific folder through
//                            the use of FTP login.
// ---------------------------------------------------
 
class Uploader
{
       private $allowed_file_types; // Array : Example: array("image/gif", "image/GIF", "image/jpg", "image/JPG", "image/jpeg", "image/JPEG", "image/pjpeg", "image/png", "image/PNG")
       private $upload_dir = ""; // Relative to file calling this class
       private $max_file_size = 0; // In bytes: 5000000 = 5 MB
       private $conn;
       private $logged_in = false;
 
       public function __construct($server, $user, $pass)
       {
              try
              {
                     if($this->conn = ftp_connect($server))
                     {
                            if($login = ftp_login($this->conn, $user, $pass))
                            {
                                   $this->logged_in = true;
                            }
                            else
                                   throw new Exception("Could not login to FTP server.");
                     }
                     else
                            throw new Exception("Could not connect to FTP server.");
              }
              catch(Exception $ex)
              {
                     return false;
              }
       }
 
       public function UploadFile($file, $uploadtype)
       {
              try
              {
                     if(!is_uploaded_file($file["tmp_name"]))
                            throw new Exception("File was not uploaded.");
                     else
                     {
                            if($file['size'] > $this->max_file_size)
                                   throw new Exception("File size over quota: ".$file['size']);
 
                            if(!in_array($file["type"], $this->allowed_file_types))
                                   throw new Exception("File uploaded has invalid extension! Extension used: ".$file["type"]);
 
                            if($this->logged_in && $this->conn)
                            {
                                   if(!ftp_put($this->conn, "/".$file["name"], $file["tmp_name"], $uploadtype))
                                          throw new Exception("Error occured while moving uploaded file.");
                            }
                            else
                                   throw new Exception("Could not connect to FTP server.");
                            return "1";
                     }
              }
              catch(Exception $ex)
              {
                     return $ex;
              }
       }
 
       public function setAllowedFileTypes($array)
       {
              $this->allowed_file_types = $array;
       }
 
       public function setUploadDirectory($string)
       {
              $this->upload_dir = $string;
       }
 
       public function setMaxSize($int)
       {
              $this->max_file_size = $int;
       }
 
       public function __destruct()
       {
              ftp_close($this->conn);
       }
}
 
?>