Last active
August 29, 2015 13:56
-
-
Save dominikwilkowski/8968469 to your computer and use it in GitHub Desktop.
TINY FILE UPLOAD ABSTRACTION LAYER
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 | |
/*****************************| UPLOAD FILES |*****************************/ | |
/** | |
* TINY FILE UPLOAD ABSTRACTION LAYER | |
* | |
* @param object $sysMsg System Message object | |
* @param string $el Name of _FILE element | |
* @param string $name Name of new file | |
* @param string $kind Kind of upload ["image"|"pdf"] | |
* @param array $dim Dimension of image [0]=width [1]=height | |
* @param string $delFile Name of file to be deleted | |
* | |
* @return array ["res"]=true|false | |
*/ | |
function uploadFile($sysMsg,$el,$name,$kind,$dim=array(),$delFile='') { | |
$r=array(); | |
$r["res"]=false; | |
if(empty($_FILES[$el]['type'])) $sysMsg->addMsg('No file selected',1); | |
else { | |
if(!is_writable(UPLOADF)) $sysMsg->addMsg('Upload folder not writeable',2); | |
else { | |
$chk=$chk2=0; | |
$im=false; | |
if($kind=="image") { //check through image conditions | |
if($_FILES[$el]['type']!="image/jpeg") $sysMsg->addMsg('Image format not supported ('.$_FILES[$el]['type'].')',2); | |
else { | |
$dimEl=getimagesize($_FILES[$el]['tmp_name']); | |
if(!empty($dim[0]) && $dimEl[0]<$dim[0] || !empty($dim[1]) && $dimEl[1]<$dim[1]) $sysMsg->addMsg('Image must be at least'.(!empty($dim[0]) ? ' '.$dim[0].'px wide' : '').(!empty($dim[1]) ? ' '.$dim[1].'px tall' : '').' (w:'.$dimEl[0].' h:'.$dimEl[1].')',2); | |
else $im=true; | |
} | |
} | |
$pdf=false; | |
if($kind=="pdf" && $_FILES[$el]['type']!="application/pdf") $sysMsg->addMsg('PDF not recognized ('.$_FILES[$el]['type'].')',2); | |
else $pdf=true; | |
if($kind=="pdf" && $pdf===true || $kind=="image" && $im===true) { //upload file | |
$chk+=move_uploaded_file($_FILES[$el]['tmp_name'],UPLOADF.$name); | |
if((int)$chk!==1) $sysMsg->addMsg('Upload has failed ('.$_FILES[$el]['tmp_name'].'=>'.UPLOADF.$name.')',2); | |
} | |
} | |
} | |
if(!empty($delFile) && (int)$chk===1) { //delete old file | |
$chk2=0; | |
$chk2+=unlink(UPLOADF.$delFile); | |
if((int)$chk2!==1) $sysMsg->addMsg('Unable to delete old image',1); | |
} | |
if((int)$chk===1) { | |
$sysMsg->addMsg('Upload successful'); | |
$r["res"]=true; | |
} | |
return $r; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment