upload php class

Upload PHP Class




Upload PHP class is developed to cut down the lengthy process of file upload in a simple manner. Upload php class is written with objective of `simple to use` in mind. Upload php class is written in php and its free



what Upload php class is

basically Upload PhP is a class which contain file uploading and error handling code and wrapped them together

why to use

Upload php class is created with the primary goal of being simple to use. Upload php cut the lengthy file uploading code to 1 line

index.php

Download the Upload.php from the links and include that file to the index.php then wait for the form to get submited then create a array of allowed extension.
call the Upload(); this method will take four parameters
1. upload file super global $_FILES[‘files’];
2. Directory in which the fill will be uploaded
3. Max allowed file upload size in bytes
4. allowed extension array

<?php 
	require_once("class/Upload.php");

	// WAIT FOR FORM TO BE SUBMITED
	if (isset($_FILES&#91;'file'&#93;)) {
		$allowedFile = array("jpg","png");
		// call the Upload class and upload file
		$upload = new Upload($_FILES&#91;'file'&#93;,"images/",1000000,$allowedFile);
		// show results
		$result = $upload->GetResult();
		print "<pre>";
		print_r($result);
		print "</pre>";
	}
?>
<form action="index.php" method="post" enctype="multipart/form-data">
	<input type="file" name="file">
	<input type="submit">
</form>

Upload.php class

<?php
	/**
	* Upload version 1.0
	* Created by Husain Saify http://hackerkernel.com
	* Date 24 august 2015
	*/
	class Upload{

		// make variables of parameters
		private $_upload,
				$_dir,
				$_size,
				$_allowed,
				$_result = array();

		function __construct($upload = array(),$dir,$size,$allowed){
			// set the paramters equal to the member variable of the upload class
			$this->_upload = $upload;
			$this->_dir = $dir;
			$this->_size = $size;
			$this->_allowed = $allowed;

			// call the upload method and upload the file braaaaaaaa
			$this->Upload();
		}

		// METHOD TO UPLOAD FILE
		private function Upload(){
			// check weather paramters are empty
			if (!empty($this->_upload) && (!empty($this->_dir)) && (!empty($this->_size)) && (!empty($this->_allowed))) {
				// check $_upload & $_allowed is an array
				if ((is_array($this->_upload)) && (is_array($this->_allowed))) {
					// get the extension of the uploaded file
					$explode = explode(".", strtolower($this->_upload['name']));
					$key = count($explode) - 1;
					$extension = $explode[$key];
					// check extension is allowed
					if (in_array($extension, $this->_allowed)) {
						// check size
						if ($this->_upload['size'] < $this->_size) {
							// upload the file
							$filename = $this->_upload['name'];
							$tmpname = $this->_upload['tmp_name'];
							if (move_uploaded_file($tmpname, $this->_dir.$filename)) {
								$this->_result['type'] = "success";
								$this->_result['message'] = "File Has been uploaded";
								$this->_result['path'] = $this->_dir.$filename;
							}else{
								$this->_result['type'] = "error";
								$this->_result['message'] = "Error in uploading file";
								$this->_result['path'] = false;
							}
						}else{
							$this->_result['type'] = "error";
							$this->_result['message'] = "Fill should be less then {$this->_size} BYTES";
							$this->_result['path'] = false;
						}
					}else{
						$this->_result['type'] = "error";
						$this->_result['message'] = "Fill Type not allowed";
						$this->_result['path'] = false;
					}
				}else{
					$this->_result['type'] = "error";
					$this->_result['message'] = "Parameters 1st and 4th should be an array";
					$this->_result['path'] = false;
				}
			}else{
				$this->_result['type'] = "error";
				$this->_result['message'] = "Parameters can not be empty";
				$this->_result['path'] = false;
			}
		}

		// METHOD TO RETURN RESULT
		public function GetResult(){
			return $this->_result;
		}
	}

?>

3 thoughts on “Upload PHP Class

Leave a Reply

Your email address will not be published. Required fields are marked *