Zend - The PHP Company




File Uploading

Add Code


FUSRFER (fus-r-for)  

Type: code fragment
Added by: ffxw0rld
Entered: 03/05/2003
Last modified: 05/12/2002
Rating: - (fewer than 3 votes)
Views: 6441
File Uploader with size restriction and file extension restriction


Ok well you see, there are people who you just don't trust with FTP. So I came up with an easy file uploading script that restricts the size and file extensions of the file the user is uploading.


I combined it with user login script, with admin control to add and delete users so if you ain't got the cookie the upload wont show but if you do itll show so use this small uploading script and add it to anything you want.


<?
$dir 
".././images"// .. means back one folder and  ./ is the current folder so its go back one and go to images/

$archive_dir "$dir";
function 
upload_form() {
          global 
$PHP_SELF;
?>
<form method="post" enctype="multipart/form-data" action="<? echo $PHP_SELF ?>">
<input type="hidden" name="action" value="upload">
Upload File!
<input type="file" name="userfile">
<input typ="submit" name=SUBMIT value="upload">
</form>
<?
}

function 
upload_file() {
     global 
$userfile$userfile_name$userfile_size$userfile_type$archive_dir$WINDIR;

     if(isset(
$WINDIR)) $userfile str_replace("\\","\", $userfile);

     
$filename = basename($userfile_name);

     if(
$userfile_size <= 0) die ("$filename is empty");
 
     if(
$userfile_size >= 102400) die ("$filename is too large <br><br> <a href='./upload2.php'>Try Uploading Again</a>"); //102400 is 100k, so its bytes/1024

     
$ext = array('.jpg','.jpeg','.txt');

     if(!in_array(strrchr(
$userfile_name,'.'),$ext))
          die ("
You cannot upload in that format");
     
     if(!@copy(
$userfile, "$archive_dir/$filename"))
          die ("
Can't copy $userfile_name to $filename.");

     if(isset($WINDIR) && !@unlink($userfile))
          die ("Can'
t delete the file $userfile_name.");


    echo "
$filename has been successfully uploaded.<br>";
    echo "
Filesize" . number_format(($userfile_size)/1024) . "<Br>";
    echo "
Filetype$userfile_type<br>";
    echo "
Fileurl: <a href='.././images/$filename'>http://SITEURL/images/$filename</a><br>";
    
echo "<br><a href='./upload3.php'>Upload Again?</a><br>";
}
?>
<html>
<head>
</head>
<body>
<?
if($action == 'upload'upload_file();
else 
upload_form();
?>
</body>
</html>


Usage Example


No example since I do not want anybody uploading to my server besides staff :D


Rate This Script





Search



This Category All Categories