AKB Forums

Go Back   AKB Forums > Technical sections > Languages, Compilers and Interpreters > Web Development
Home Register Blogs FAQ Members List Calendar Downloads Arcade Mark Forums Read

Web Development Java/PHP/Perl/ASP and more

Troubles when posting message? Click here! :: Проблемы с отправлением сообщения? Нажмите сюда!

Reply
 
LinkBack Thread Tools Display Modes
Old Aug 9, 2002, 18:56   #1
Administrator
 
acid's Avatar
 
Join Date: Sep 2001
Location: South Korea, Gumi
Posts: 7,277
Blog Entries: 15
Rep Power: 10
Reputation: 372
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">
__________________

http://big-acid.blogspot.com/
acid is offline   Reply With Quote Quote selected
Old Aug 9, 2002, 20:03   #2
Moderator
 
Mono's Avatar
 
Join Date: Oct 2001
Location: Yerevan
Posts: 5,401
Blog Entries: 1
Rep Power: 9
Reputation: 102
Post

Inch lezvov???
__________________
---------------
Արատտայի ու Խալդեյի հովանավոր .
Mono is offline   Reply With Quote Quote selected
Old Aug 9, 2002, 20:56   #3
Administrator
 
acid's Avatar
 
Join Date: Sep 2001
Location: South Korea, Gumi
Posts: 7,277
Blog Entries: 15
Rep Power: 10
Reputation: 372
Post

Перл, PHP
__________________

http://big-acid.blogspot.com/
acid is offline   Reply With Quote Quote selected
Old Aug 10, 2002, 07:20   #4
Студент
 
Join Date: Jun 2002
Location: Yerevan
Posts: 258
Rep Power: 7
Reputation: 10
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
Eddi is offline   Reply With Quote Quote selected
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 12:27.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
This board was founded on September 29, 2001
Powered by Viper Internet

Affordable Web Hosting | ParevNet

Buy text link