| <?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); |
| } |
| } |
| |
| ?> |