Last active
December 4, 2019 09:27
-
-
Save remoharsono/38261eda0b1efd87f8d4b8ad228e8fbc to your computer and use it in GitHub Desktop.
PHP :: Simple File Upload
This file contains hidden or 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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>.</title> | |
</head> | |
<body> | |
<?php | |
// namespace Remo\Upload; | |
/* | |
sample output ON SUCCESS: | |
Array | |
( | |
[0] => Array | |
( | |
[filename] => uploaded_document.pdf | |
[new_filename] => 20191204155419_5de7743b01200_FUN9AudW.pdf | |
[size] => 452,373 | |
[status] => OK | |
) | |
) | |
*/ | |
class Upload { | |
var $version = '0.1.0'; | |
var $uploadFolder; | |
var $uploadFile; | |
var $arrResults; | |
var $allowedFileExtensions; | |
var $allowedMimeTypes; | |
var $maxFileSize; // in MegaBytes | |
function __construct($uploadFile, $config = null) { | |
$this->uploadFile = $uploadFile; | |
if ($config == null) { | |
$this->uploadFolder = 'uploads/'; | |
$this->allowedFileExtensions = $this->getDefaultAllowedFileExtensions(); | |
$this->allowedMimeTypes = $this->getDefaultAllowedMimeTypes(); | |
$this->maxFileSize = 25; | |
} else { | |
$this->uploadFolder = $config['uploadFolder']; | |
$this->allowedFileExtensions = $config['allowedFileExtensions']; | |
$this->allowedMimeTypes = $config['allowedMimeTypes']; | |
$this->maxFileSize = $config['maxFileSize']; | |
} | |
} | |
function getDefaultAllowedFileExtensions() { | |
return array( | |
'pdf', | |
'jpg', | |
'jpeg', | |
'png', | |
'gif', | |
'mp3', | |
'mpeg', | |
'odp', | |
'ods', | |
'odt', | |
'tif', | |
'tiff', | |
'font/ttf', | |
'vsd', | |
'webm', | |
'webp', | |
'avi', | |
'mp4', | |
'ppt', | |
'pptx', | |
'doc', | |
'docx', | |
'xls', | |
'xlsx', | |
'zip', | |
'txt' | |
); | |
} | |
function getDefaultAllowedMimeTypes() { | |
return array( | |
'application/pdf', | |
'image/jpeg', | |
'image/png', | |
'image/gif', | |
'audio/mpeg', | |
'application/vnd.oasis.opendocument.presentation', | |
'application/vnd.oasis.opendocument.spreadsheet', | |
'application/vnd.oasis.opendocument.text', | |
'image/tiff', | |
'font/ttf', | |
'application/vnd.visio', | |
'video/webm', | |
'image/webp', | |
'video/avi', | |
'video/mp4', | |
'application/vnd.ms-powerpoint', | |
'application/vnd.openxmlformats-officedocument.presentationml.presentation', | |
'application/msword', | |
'application/msword', | |
'application/vnd.ms-excel', | |
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', | |
'application/zip', | |
'text/plain' | |
); | |
} | |
function generateRandomString($length = 10) { | |
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; | |
$charactersLength = strlen($characters); | |
$randomString = ''; | |
for ($i = 0; $i < $length; $i++) { | |
$randomString .= $characters[rand(0, $charactersLength - 1)]; | |
} | |
return $randomString; | |
} | |
function random_str($length = 64, $keyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') { | |
if ($length < 1) { | |
throw new \RangeException("Length must be a positive integer"); | |
} | |
$pieces = array(); | |
$max = mb_strlen($keyspace, '8bit') - 1; | |
for ($i = 0; $i < $length; ++$i) { | |
$pieces []= $keyspace[random_int(0, $max)]; | |
} | |
return implode('', $pieces); | |
} | |
function getNewFilename($oldFilename) { | |
$uniqueDateTime = date("YmdHis") . '_' . uniqid(); | |
$filename = $uniqueDateTime . '_' . $this->random_str(8) . '.' . $this->getExtension($oldFilename); | |
return $filename; | |
} | |
function getExtension($filename) { | |
if (strpos($filename, ".") !== false) { | |
$pos = strrpos($filename, "."); | |
return substr($filename, $pos+1, strlen($filename)); | |
} else { | |
return ''; | |
} | |
} | |
function getMimeType($filename) { | |
if (function_exists('mime_content_type')) { | |
return mime_content_type($filename); | |
} elseif (function_exists('finfo_open')) { | |
$finfo = finfo_open(FILEINFO_MIME_TYPE); | |
$mimeType = finfo_file($finfo, $filename); | |
finfo_close($finfo); | |
return $mimeType; | |
} else { | |
return ''; | |
} | |
} | |
function isAllowedExtension($filename) { | |
if (in_array($this->getExtension($filename), $this->allowedFileExtensions)) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
function isAllowedMimeType($filename) { | |
if (in_array($this->getMimeType($filename), $this->allowedMimeTypes)) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
function process() { | |
$maxFileSize = $this->maxFileSize * 1000000; | |
$numFiles= count($this->uploadFile['name']); | |
$arrResults = array(); | |
for ( $counter=0 ; $counter < $numFiles ; $counter++ ) { | |
$tmp_file = $this->uploadFile['tmp_name'][$counter]; | |
if ($tmp_file != ""){ | |
$filename = $this->uploadFile['name'][$counter]; | |
$filesize = $this->uploadFile['size'][$counter]; | |
// allowed extension ? | |
if ($this->isAllowedExtension($filename)) { | |
// size below max size ? | |
if ($filesize < $maxFileSize) { | |
if ($this->isAllowedMimeType($tmp_file)) { | |
do { | |
$newFilename = $this->getNewFilename($filename); | |
$newFilePath = $this->uploadFolder . '/' . $newFilename; | |
} while (file_exists($newFilePath)); | |
if (@move_uploaded_file($tmp_file, $newFilePath)) { | |
$arrResults[$counter]['filename'] = $filename; | |
$arrResults[$counter]['new_filename'] = $newFilename; | |
$arrResults[$counter]['size'] = number_format($filesize); | |
$arrResults[$counter]['status'] = 'OK'; | |
} else { | |
$arrResults[$counter]['filename'] = $filename; | |
$arrResults[$counter]['size'] = number_format($filesize); | |
$arrResults[$counter]['status'] = 'ERROR'; | |
$arrResults[$counter]['decription'] = 'FAILED_MOVING_FILE'; | |
} | |
} else { | |
$arrResults[$counter]['filename'] = $filename; | |
$arrResults[$counter]['size'] = number_format($filesize); | |
$arrResults[$counter]['status'] = 'ERROR'; | |
$arrResults[$counter]['decription'] = 'DISALLOWED_MIME_TYPE'; | |
} | |
} else { | |
$arrResults[$counter]['filename'] = $filename; | |
$arrResults[$counter]['size'] = number_format($filesize); | |
$arrResults[$counter]['status'] = 'ERROR'; | |
$arrResults[$counter]['decription'] = 'EXCEEDS_MAX_FILE_SIZE'; | |
} | |
} else { | |
$arrResults[$counter]['filename'] = $filename; | |
$arrResults[$counter]['size'] = number_format($filesize); | |
$arrResults[$counter]['status'] = 'ERROR'; | |
$arrResults[$counter]['decription'] = 'DISALLOWED_FILE_EXTENSION'; | |
} | |
} | |
} | |
$this->arrResults = $arrResults; | |
} | |
} | |
if (isset($_POST['submit'])) { | |
$upload_folder = "uploads"; | |
$max_file_size = 50; // in megabytes | |
$allowed_file_extensions = array( | |
'pdf', | |
'jpg', | |
'jpeg', | |
'png', | |
'gif', | |
'mp3', | |
'mpeg', | |
'odp', | |
'ods', | |
'odt', | |
'tif', | |
'tiff', | |
'font/ttf', | |
'vsd', | |
'webm', | |
'webp', | |
'avi', | |
'mp4', | |
'ppt', | |
'pptx', | |
'doc', | |
'docx', | |
'xls', | |
'xlsx', | |
'zip', | |
'txt' | |
); | |
$allowed_mime_types = array( | |
'application/pdf', | |
'image/jpeg', | |
'image/png', | |
'image/gif', | |
'audio/mpeg', | |
'application/vnd.oasis.opendocument.presentation', | |
'application/vnd.oasis.opendocument.spreadsheet', | |
'application/vnd.oasis.opendocument.text', | |
'image/tiff', | |
'font/ttf', | |
'application/vnd.visio', | |
'video/webm', | |
'image/webp', | |
'video/avi', | |
'video/mp4', | |
'application/vnd.ms-powerpoint', | |
'application/vnd.openxmlformats-officedocument.presentationml.presentation', | |
'application/msword', | |
'application/msword', | |
'application/vnd.ms-excel', | |
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', | |
'application/zip', | |
'text/plain' | |
); | |
$config = array( | |
'uploadFolder' => $upload_folder, | |
'maxFileSize' => $max_file_size, | |
'allowedFileExtensions' => $allowed_file_extensions, | |
'allowedMimeTypes' => $allowed_mime_types | |
); | |
$upload = new Upload($_FILES['myfile'], $config); | |
// atau | |
// $upload->upload_folder = 'uploads/'; | |
// $upload->max_file_size = 2; | |
// $upload->allowed_file_extensions = $allowed_file_extensions; | |
// $upload->allowed_mime_types = $allowed_mime_types; | |
$upload->process(); | |
echo json_encode($upload->arrResults); | |
} else { | |
?> | |
<form method="post" enctype="multipart/form-data" action="upload2.php"> | |
<input type="file" name="myfile[]" id="myfile" multiple=""> | |
<input name="submit" type="submit" value="Upload"> | |
</form> | |
<?php | |
} | |
?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment