Wednesday, June 6, 2012

PHP File Upload Class

uploader.php

[sourcecode language="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;
}

}




[/sourcecode]

How to use this uploader class

[sourcecode language="php"]
<?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>
[/sourcecode]

No comments:

Post a Comment

How to enable CORS in Laravel 5

https://www.youtube.com/watch?v=PozYTvmgcVE 1. Add middleware php artisan make:middleware Cors return $next($request) ->header('Acces...