Created
July 31, 2011 17:13
-
-
Save JunaidQadirB/1116980 to your computer and use it in GitHub Desktop.
The Uploader class is a simple php script that makes file uploads a bit easier.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* The Uploader class is a simple php script that makes file | |
* uploads a bit easier. | |
* @author Junaid Qadir Baloch ([email protected]) | |
* @version 0.1 10:18 PM 7/30/2011 | |
* | |
*/ | |
class Uploader | |
{ | |
private $allowedFileTypes; | |
private $maxFileSize = 1048576; | |
private $fileInputBoxName; | |
private $fileName; | |
private $fileType; | |
private $fileTmpName; | |
private $fileError; | |
private $fileSize; | |
private $uploadPath; | |
function __construct($fileInputBoxName, $uploadPath = "uploads", $allowedFileTypes) | |
{ | |
if (is_array ( $allowedFileTypes )) | |
{ | |
$this->allowedFileTypes = $allowedFileTypes; | |
} else | |
{ | |
$this->allowedFileTypes [] = $allowedFileTypes; | |
} | |
$this->uploadPath = $uploadPath; | |
$this->fileInputBoxName = $fileInputBoxName; | |
} | |
/** | |
* | |
* @return array | |
*/ | |
public function upload() | |
{ | |
ini_set ( "file_uploads", "On" ); | |
$name = $_FILES [$this->fileInputBoxName] ["name"]; | |
$type = $_FILES [$this->fileInputBoxName] ["type"]; | |
$tmp = $_FILES [$this->fileInputBoxName] ["tmp_name"]; | |
$error = $_FILES [$this->fileInputBoxName] ["error"]; | |
$size = $_FILES [$this->fileInputBoxName] ["size"]; | |
$maxFileSize = $this->maxFileSize; | |
if ($size > $maxFileSize) | |
{ | |
$error = 2; | |
} | |
if ($error == 0) | |
{ | |
if (in_array ( $type, $this->allowedFileTypes )) //== "application/msword" or $type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document" or $type == "application/pdf") | |
{ | |
$uploadsPath = realpath ( $this->uploadPath ); | |
$timeStamp=time(); | |
$name=$timeStamp."_".$name; | |
if (copy ( $tmp, $uploadsPath . DIRECTORY_SEPARATOR . $name )) | |
{ | |
$retArray [uploadAction] = array ( | |
status => "Success", | |
message => "", | |
contentType => $type, | |
fileName => $name, | |
filePath => $uploadsPath . DIRECTORY_SEPARATOR . $name | |
); | |
return $retArray; | |
} | |
} else | |
{ | |
$retArray [uploadAction] = array ( | |
status => "Error", | |
message => "Selected file type is not allowed." | |
); | |
return $retArray; | |
} | |
} elseif ($error == 2) | |
{ | |
$fz = $this->maxFileSize * 1024 * 1024; | |
$retArray [uploadAction] = array ( | |
status => "Error", | |
message => "The uploaded file exceeds the maximum allowed size ({$fz} MB)." | |
); | |
return $retArray; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment