Code:
<title>Maaking.Com File Uploader</title>
<script language="JavaScript" type="text/javascript">
<!-- ;
var newwindow;
var wheight = 0, wwidth = 0;
function popitup5(url, title, iwidth, iheight, colour) {
var pwidth, pheight;
if ( !newwindow || newwindow.closed ) {
pwidth=iwidth+30;
pheight=iheight+30;
newwindow=window.open('','htmlname','width=' + pwidth +',height=' +pheight + ',resizable=1,top=50,left=10');
wheight=iheight;
wwidth=iwidth;
}
if (wheight!=iheight || wwidth!=iwidth ) {
pwidth=iwidth+30;
pheight=iheight+60;
newwindow.resizeTo(pwidth, pheight);
wheight=iheight;
wwidth=iwidth;
}
newwindow.document.clear();
newwindow.focus();
newwindow.document.writeln('<html> <head> <title>' + title + '<\/title> <\/head> <body bgcolor= \"' + colour + '\"> <center>');
newwindow.document.writeln('[img] + url + [/img]');
newwindow.document.writeln('<\/center> <\/body> <\/html>');
newwindow.document.close();
newwindow.focus();
}
// Routines to tidy up popup windows when page is left
// Call with an onUnload="tidy5()" in body tag
function tidy5() {
if (newwindow && !newwindow.closed) { newwindow.close(); }
}
</script>
<?php
/*
Author: Mohammed Ahmed(M@@king)
Version: 1.0
Date: 10.Oct.2004
----------------------------
Last Update: 15.Nov.2004
----------------------------
E-mail: m@maaking.com
MSN : m@maaking.com
WWW : http://www.maaking.com
---Description -----------------------------------------------------
The Super Global Variable $_FILES is used in PHP 4.x.x.
$_FILES['upload']['size'] ==> Get the Size of the File in Bytes.
$_FILES['upload']['tmp_name'] ==> Returns the Temporary Name of the File.
$_FILES['upload']['name'] ==> Returns the Actual Name of the File.
$_FILES['upload']['type'] ==> Returns the Type of the File.
So if I uploaded the file 'test.doc', the $_FILES['upload']['name']
would be 'phptut.doc' and $_FILES['upload']['type'] would be 'application/msword'.
---------------------------------------------------------------------*/
//**********************************************************************//
// $_FILES['filetoupload'] is the value of //
// file field from the form. <input type="file" name="filetoupload"> //
//**********************************************************************//
// this is the upload dir where files will go.
//Don't remove the /
//Chmod it (777)
$upload_dir = "images/"; //change to whatever you want.
//51200 bytes = 50KB
$size_bytes = 51200; //File Size in bytes (change this value to fit your need)
$extlimit = "yes"; //Do you want to limit the extensions of files uploaded (yes/no)
$limitedext = array(".gif",".jpg",".png",".jpeg"); //Extensions you want files uploaded limited to. also you can use: //array(".gif",".jpg",".jpeg",".png",".txt",".nfo",".doc",".rtf",".htm",".dmg",".zip",".rar",".gz",".exe");
//check if the directory exists or not.
if (!is_dir("$upload_dir")) {
die ("The directory ($upload_dir) doesn't exist");
}
//check if the directory is writable.
if (!is_writeable("$upload_dir")){
die ("The directory ($upload_dir) is NOT writable, Please CHMOD (777)");
}
if($uploadform) // if you clicked the (Upload File) button. "If you submitted the form" then upload the file.
{//begin of if($uploadform).
//check if no file selected.
if (!is_uploaded_file($_FILES['filetoupload']['tmp_name']))
{
echo "Error: Please select a file to upload!.
»<a href=\"$_SERVER[PHP_SELF]\">back</a>";
exit(); //exit the script and don't do anything else.
}
//Get the Size of the File
$size = $_FILES['filetoupload']['size'];
//Make sure that file size is correct
if ($size > $size_bytes)
{
$kb = $size_bytes / 1024;
echo "File Too Large. File must be $kb KB.
»<a href=\"$_SERVER[PHP_SELF]\">back</a>";
exit();
}
//check file extension
$ext = strrchr($_FILES['filetoupload'][name],'.');
if (($extlimit == "yes") && (!in_array($ext,$limitedext))) {
echo("Wrong file extension. ");
exit();
}
// $filename will hold the value of the file name submetted from the form.
$filename = $_FILES['filetoupload']['name'];
// Check if file is Already EXISTS.
if(file_exists($upload_dir.$filename)){
echo "Oops! The file named $filename already exists.
»<a href=\"$_SERVER[PHP_SELF]\">back</a>";
exit();
}
//Move the File to the Directory of your choice
//move_uploaded_file('filename','destination') Moves afile to a new location.
if (move_uploaded_file($_FILES['filetoupload']['tmp_name'],$upload_dir.$filename)) {
//tell the user that the file has been uploaded and make him alink.
echo "File (back";
exit();
}
// print error if there was a problem moving file.
else
{
//Print error msg.
echo "There was a problem moving your file.
»<a href=\"$_SERVER[PHP_SELF]\">back</a>";
exit();
}
}//end of if($uploadform).
#---------------------------------------------------------------------------------#
// If the form has not been submitted, display it!
else
{//begin of else
?>
<h3>::Browse a File to Upload:</h3>
- Allowed Extensions:
<?
// print the file extensions
for($i=0;$i<count($limitedext);$i++)
{
if (($i<>count($limitedext)-1))$commas=", ";else $commas="";
list($key,$value)=each($limitedext);
echo $value.$commas;
}
?>
- Max File Size = <?echo $size_bytes / 1024; ?> KB
<form method="post" enctype="multipart/form-data" action="<?php echo $PHP_SELF ?>">
<input type="file" name="filetoupload">
<input type="hidden" name="MAX_FILE_SIZE" value="<?echo $size_bytes; ?>">
<input type="Submit" name="uploadform" value="Upload File">
</form>
<?
}//end of else
///////////////////////////////////////////////////////////
function del()
{
global $upload_dir, $file;
//$value_de = base64_decode([file]);
@unlink($upload_dir."/$file");
echo"<h2><font color=red>The File ($file) was deleted!</font></h2>";
echo "<meta http-equiv=Refresh content=1;url=index.php>";
}
////////////////////////////////////////////////////////////////
if ( $action == del )
{
del();
}
/*______________________________________________________________________________*/
// Here is the most interesting part.
// it views the directory contents.....i'll disscuss next version. (ver 2.0)
echo "
<hr>
Current Uploaded Files:
";
?>
<table border="1" bordercolor="#C0C0C0" cellspacing="1">
<tr>
<td bgcolor="#F4F4F4">File Name</td>
<td bgcolor="#F4F4F4">File Size</td>
</tr>
<?
$rep=opendir($upload_dir);
while ($file = readdir($rep)) {
if($file != '..' && $file !='.' && $file !=''){
if (!is_dir($file)){
$imgsize = getimagesize ($upload_dir."".$file);
// print the file name and then make a link.
// javascript:img('$upload_dir$file','$imgsize[0]','$imgsize[1]')
echo "<tr><td> <a href=\"javascript:popitup5('$upload_dir$file','$file', $imgsize[0], $imgsize[1],'white')\">
<img src=$upload_dir$file border=0 width=100>
</a>
</td>";
#------------begin of file size.
//print the file size.
$file_size = filesize($upload_dir."".$file);
if ($file_size >= 1048576)
{
$show_filesize = number_format(($file_size / 1048576),2) . " MB";
}
elseif ($file_size >= 1024)
{
$show_filesize = number_format(($file_size / 1024),2) . " KB";
}
elseif ($file_size >= 0)
{
$show_filesize = $file_size . " bytes";
}
else
{
$show_filesize = "0 bytes";
}
echo "<td> $show_filesize";
echo "
<p align=center>";
echo"[<a title=\"Delete File\" href=\"javascript:;\" onClick=\"cf=confirm('Are you sure you want to delete?');if (cf)window.location='index.php?action=del&file=$file'; return false;\">Del</a>]";
echo "</p></td></tr>";
#------------end of file size.
}
}
}
closedir($rep);
clearstatcache();
?>
</table>
<p align=right>
Script by: <a href=http://www.maaking.com>maaking.com</a>
</p>
Bookmarks