Go Back   Armenian Knowledge Base > Technical sections > Webmaster Zone > Web Development

Reply
 
Thread Tools

Upload Script
Old 09.08.2002, 18:56   #1
Moderator
 
acid's Avatar
 
Join Date: 09 2001
Location: South Korea, Gumi
Posts: 7,699
Blog Entries: 16
Rep Power: 7
Post Upload Script

Looking for advanced upload script with the following mandatory features:

</font><ul type="square"><li><font size="2" face="MS Sans Serif, Verdana, Helvetica, sans-serif">Ability to restrict upload file size and type
</font></li><li><font size="2" face="MS Sans Serif, Verdana, Helvetica, sans-serif">Easy to install
</font></li></ul><font size="2" face="MS Sans Serif, Verdana, Helvetica, sans-serif">

Old 09.08.2002, 20:03   #2
Moderator
 
Mono's Avatar
 
Join Date: 10 2001
Location: Yerevan
Posts: 5,466
Blog Entries: 1
Rep Power: 6
Post

Inch lezvov???
__________________
---------------
Արատտայի ու Խալդեյի հովանավոր .

Old 09.08.2002, 20:56   #3
Moderator
 
acid's Avatar
 
Join Date: 09 2001
Location: South Korea, Gumi
Posts: 7,699
Blog Entries: 16
Rep Power: 7
Post

Перл, PHP

Old 10.08.2002, 07:20   #4
Студент
 
Join Date: 06 2002
Location: Yerevan
Posts: 258
Rep Power: 0
Post

Quote:
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>
<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

echo '<center>File Upload Sucess!</center>';
return $ErrNo;
} //function DoFileUpload($InputFile, $MaxSize, $Path, $ErrorFunction,
$ExtsOk, $ForceFilename, $OverwriteOk)

function GetExt($Filename) {
$RetVal = explode ( '.', $Filename);
return $RetVal[count($RetVal)-1];
}
Original here - http://www.php.net/manual/en/features.file-upload.php
If you want to add file type recognition there is $FileType defined, which you could use.

P.S. If PHP < 4.1 use $HTTP_POST_FILES instead of $_FILES.
__________________
http://www.d-brane.com
Reply




Реклама:
реклама
Buy text link .

All times are GMT. The time now is 01:49.
Top

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.