note 21332 added to features.file-upload

From: Date: Wed, 08 May 2002 21:06:20 +0000
Subject: note 21332 added to features.file-upload
Groups: php.notes 
Request: Send a blank email to php-notes+get-30355@lists.php.net to get a copy of this message
Here is a fully functional File Upload Handler function. You simply pass it the name that you typed on the form, the upload dir (and others if your advanced,) and viola! call like so: $OkExt = Array('php', 'htm', 'html') DoFileUpload('Userfile', 1024, './upload', '', $OkExt , '', '') $ForceFilename is if you want to use this name instead of whats in the $_FILES var. $Overwriteok - set to anything but '' for yes $ErrorFunction - set to the name of a function you defined, or '' to use default. Need help? ask again. Not that you may have to fix word wrap... ---------------------------- function DoFileUpload($InputFile, $MaxSize, $Path, $ErrorFunction, $ExtsOk, $ForceFilename, $OverwriteOk) { //Copyright CBWhiz $ErrNo = -1; $TempFile = $_FILES[$InputFile]['tmp_name']; $FileSize = $_FILES[$InputFile]['size']; $FileName = $_FILES[$InputFile]['name']; $FileType = $_FILES[$InputFile]['type']; if (strlen($ForceFilename)) { $FileName = $ForceFilename; } if (!function_exists($ErrorFunction)) { if (!function_exists('DoFileUploadDefErrorHandle')) { function DoFileUploadDefErrorHandle($ErrorNumber, $ErrorText) { echo "<center><font color=red><b>Error $ErrorNumber: $ErrorText</b></font></center>"; } } $ErrorFunction = 'DoFileUploadDefErrorHandle'; } echo <<<HTML <hr><br> <center>Uploading $InputFile:<hr width=35%> <table> <tr><td>Filename:</td><td>$FileName</td></tr> <tr><td>File Size:</td><td>$FileSize</td></tr> <tr><td>Temporary File:</td><td>$TempFile</td></tr> <tr><td>File MIME Type:</td><td>$FileType</td></tr> </table> <hr width=35%> </center> HTML; if($TempFile == 'none' || $TempFile == '') { $ErrorTxt = "This File was unspecified."; $ErrNo = 1; $ErrorFunction($ErrNo, $ErrorTxt); return $ErrNo; } if(!is_uploaded_file($TempFile)) { $ErrorTxt = "File Upload Attack, Filename: \"$FileName\""; $ErrNo = 2; $ErrorFunction($ErrNo, $ErrorTxt); return $ErrNo; } //if(!is_uploaded_file($TempFile)) if($FileSize == 0) { $ErrorTxt = 'The file you attempted to upload is zero length!'; $ErrNo = 3; $ErrorFunction($ErrNo, $ErrorTxt); return $ErrNo; } //$FileSize == 0 $TheExt = GetExt($FileName); foreach ($ExtsOk as $CurNum => $CurText) { if ($TheExt == $CurText) { $FileExtOk = 1; } } if($FileExtOk != 1) { $ErrorTxt = 'You attempted to upload a file with a disallowed extention!'; $ErrNo = 4; $ErrorFunction($ErrNo, $ErrorTxt); return $ErrNo; } if($FileSize > $MaxSize) { $ErrorTxt = 'The file you attempted to upload exceeded the maximum file size of' . ($MaxSize / 1024) . 'kb.'; $ErrNo = 5; $ErrorFunction($ErrNo, $ErrorTxt); return $ErrNo; } //if($FileSize > $MaxSize) if(file_exists($Path.$FileName) && !strlen($OverwriteOk)) { $ErrorTxt = 'The file you attempted to upload already exists. Please specify a new filename.'; $ErrNo = 6; $ErrorFunction($ErrNo, $ErrorTxt); return $ErrNo; } //----------------------------------- //-------Actual Uploading Here move_uploaded_file ($TempFile, $Path.$FileName); chmod ($Path.$FileName, 0644); //Remove if your webserver hates you :D echo '<center>File Upload Sucess!</center><br>'; return $ErrNo; } //function DoFileUpload($InputFile, $MaxSize, $Path, $ErrorFunction, $ExtsOk, $ForceFilename, $OverwriteOk) function GetExt($Filename) { $RetVal = explode ( '.', $Filename); return $RetVal[count($RetVal)-1]; } -- http://www.php.net/manual/en/features.file-upload.php http://master.php.net/manage/user-notes.php?action=edit+21332 http://master.php.net/manage/user-notes.php?action=delete+21332 http://master.php.net/manage/user-notes.php?action=reject+21332

« previous php.notes (#30355) next »