Last active
February 5, 2018 19:12
-
-
Save samuel-fonseca/4fdcf0f892b853a8196beacb7be34416 to your computer and use it in GitHub Desktop.
A simple script to process file uploads from Dropzone.js extension. Just call the function `uploadFile()` and add the desired params.
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
<?php | |
/** | |
* @var $file is an array with file data | |
* @var $targetDir string with path to directory | |
* @var $maxFileSize int with max Byte size | |
* @var $_allowedFileTypes a comma separated string with all the allowed extensions | |
*/ | |
function uploadFile(array $file, $targetDir, $maxFileSize = 100000, $_allowedFileTypes = '(jpg)') | |
{ | |
// create var from array | |
$filename = (isset($file['name'])) ? $file['name'] : ''; | |
$filetype = (isset($file['type'])) ? $file['type'] : ''; | |
$file_tmpname = (isset($file['tmp_name'])) ? $file['tmp_name'] : ''; | |
$fileerror = (isset($file['error'])) ? $file['error'] : ''; | |
$filesize = (isset($file['size'])) ? $file['size'] : ''; | |
/** | |
* Get exension from File | |
* | |
* @var $filename string with the full file name | |
*/ | |
function findExtension($filename) | |
{ | |
if(is_string($filename)) | |
{ | |
$filename = strtolower($filename); | |
$exts = split("[/\\.]", $filename); | |
$n = count($exts)-1; | |
$exts = $exts[$n]; | |
return $exts; | |
} | |
else | |
{ | |
trigger_error('File name was not a string'); | |
print_r($filename); | |
return false; | |
} | |
} | |
// check extension | |
$ext = findExtension($filename); | |
// check if extension is allowed | |
$allowedFiles = preg_match("/".$_allowedFileTypes."$/i", $ext); | |
// setup errors | |
// create checks and balances | |
$err = array( | |
'complete' => 'false', | |
'is_error' => 'warning' | |
); | |
// maxsize == 100kB | |
$err['message'] = ($filesize > $maxFileSize) ? "File is too large ($filesize). Please make sure your file is less than $maxFileSize" : null; | |
// filetype == only $_allowedFileTypes | |
$err['message'] = ($allowedFiles != true) ? "File format is incorrect ($filetype). Please make sure your image is JPEG." : null; | |
if( isset($err['message']) ) | |
{ | |
if( $err['message'] !== null ) | |
{ | |
trigger_error($err['message']); | |
return false; # stop function here | |
} | |
} | |
// if the file extension is allowed | |
if( $allowedFiles == true ) | |
{ | |
// setup target file | |
$target_file = $targetDir . $filename; | |
// move the file from tmp folder to target folder | |
$move_file = move_uploaded_file($file_tmpname, $target_file); | |
// if moved sucess | |
if ($move_file) | |
{ | |
$message = array( | |
'complete' => 'true', | |
'is_error' => 'success', | |
'message' => 'Image uploaded successfully.' | |
); | |
} | |
else | |
{ | |
$message = array( | |
'complete' => 'false', | |
'is_error' => 'danger', | |
'message' => 'Image NOT uploaded successfully.' | |
); | |
} | |
return (json_encode($message)); | |
} | |
else | |
{ | |
trigger_error('Invalid file format'); | |
return false; | |
} | |
} | |
// upload.php | |
if(isset($_POST)) | |
{ | |
if( isset($_FILES) ) | |
{ | |
$upload = uploadFile( | |
$_FILES['file'], # file should be an array | |
'_uploads/', # target folder for upload | |
125000, # max upload size in Bytes (currently set to 1Mb) | |
'jpg,png,gif' # allowed files should be separated by `,` | |
); | |
print_r($upload); | |
} | |
else | |
{ | |
echo 'Incorrect data passed.'; | |
} | |
} | |
else | |
{ | |
echo 'Nothing to see here.'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment