RSS

PHP File Upload Class

06 Jun

uploader.php



class Uploader
{
private $destinationPath;
private $errorMessage;
private $extensions;
private $maxSize;
private $uploadName;
private $seqnence;
private $renamedState = 0;

public function setDir($path)
{
$this->destinationPath = $path;
}

public function setMaxSize($sizeMB)
{
$this->maxSize = $sizeMB * (1024*1024);
}

public function setExtensions($options)
{
$this->extensions = $options;
}

public function getExtension($string)
{
$ext = "";
try
{
$parts = explode(".",$string);
$ext = strtolower($parts[count($parts)-1]);
}
catch(Exception $c)
{
$ext = "";
}
return $ext;
}

public function setMessage($message)
{
$this->errorMessage = $message;
}

public function getMessage()
{
return $this->errorMessage;
}

public function getUploadName()
{
return $this->uploadName;
}

public function setSequence($seq)
{
$this->imageSeq = $seq;
}

public function getRandom()
{
return strtotime(date('Y-m-d H:iConfused')).rand(1111,9999).rand(11,99).rand(111,999);
}

public function getRenamedState()
{
return $this->renamedState;
}

public function renameFile($state,$newName)
{
$this->renamedState = $state;
$this->uploadName = $newName;
}

public function deleteUploaded()
{
unlink($this->destinationPath.$this->uploadName);
}

public function uploadFile($fileBrowse)
{
$result = false;
$size = $_FILES[$fileBrowse]["size"];
$name = $_FILES[$fileBrowse]["name"];
$ext = $this->getExtension($name);
if(!is_dir($this->destinationPath))
{
$this->setMessage("Destination folder is not a directory ");
}
else if(!is_writable($this->destinationPath))
{
$this->setMessage("Destination is not writable !");
}
else if(empty($name))
{
$this->setMessage("File not selected ");
}
else if($size>$this->maxSize)
{
$this->setMessage("Too large file !");
}
else if(in_array($ext,$this->extensions))
{
switch ($this->getRenamedState())
{
case 0: //Keep original file name
$this->uploadName= $name;
break;
case 1: //Rename file with randomly-generated name
$this->uploadName = $this->imageSeq."-".substr(md5(rand(1111,9999)),0,8).$this->getRandom().rand(1111,1000).rand(99,9999).".".$ext;
break;
case 2: //Rename file with given name when calling renameFile() function
$this->uploadName.= ".".$ext;
break;
default:
$this->uploadName= $name;
}


if(move_uploaded_file($_FILES[$fileBrowse]["tmp_name"],$this->destinationPath.$this->uploadName))
{
$result = true;
}
else
{
$this->setMessage("Upload failed");
}
}
else
{
$this->setMessage("Invalid file format!");
}
return $result;
}

}




How to use this uploader class

<?php
if(isset($_POST['btnSub']))
{
require_once('uploader.php');
$uploader = new Uploader();
$uploader->setDir('public/img');
$uploader->setExtensions(array('jpg','jpeg','png','gif')); //allowed extensions list
$uploader->setMaxSize(.5); //set max file size to be allowed in MB

//Select one of the following alternatives
//$uploader->renameFile(0,""); //Keep original file name
//$uploader->renameFile(1,""); //Rename file with randomly-generated name
//$uploader->renameFile(2,"sample"); // Rename file with 'sample'

if($uploader->uploadFile('txtFile'))//specify file field
{
$image = $uploader->getUploadName(); //get uploaded file name
}
else
{
echo $uploader->getMessage(); //get upload error message
}

}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>File Upload Example</title>
</head>

<body>
<form action="" method="post" enctype="multipart/form-data">
<input name="txtFile" type="file" />
<input name="btnSub" type="submit" />
</form>
</body>
</html>
Advertisements
 
Leave a comment

Posted by on June 6, 2012 in OOP, PHP

 

Tags:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: