Location: PHPKode > scripts > Basic Uploader > basic_uploader/index.php
<?php

# COPYRIGHT
# Basic Uploader © 2012 Scott Connell - All Rights Reserved.
# Source and latest version: http://scottconnell.orgfree.com
# Release Date: 2012/09/11
##########################################################
# This program is free software; you can redistribute it and/or modify it under the
# terms of the GNUv2 General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later version.
##########################################################
# SET VARIABLES #################################
# Set how many simultaneous uploads to allow.

$number_of_uploads = 3;

# Set allowed file types, lowercase without period (.)

$allowed_file_types =	 array("jpg","gif","png","txt");

# Change the upload_folder path, with trailing slash, 
# to your full directory path if neccessary, and set 
# permissions (chmod) to 777.

$upload_folder = "./uploads/";

# Set maximum file upload size in kilobytes.

$max_size_in_kb = 512;

# Set 1 to rename files, 0 to keep original file names.

$rename_files = 1;

# END SETTING VARIABLES #################

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<title>Basic Uploader</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style type="text/css">html, input, select { font: 1.2em Georgia, serif; }</style>
</head>

<body>

<h2>Basic Uploader - Allow file uploads while controlling file types</h2>

<?php

function printForm()
{
global $allowed_file_types,$number_of_uploads;

print "<p>Allowed file types: ." . implode($allowed_file_types, " ."). "</p>\n";

print "<form action=\"". $_SERVER["PHP_SELF"] ."\" method=\"post\" enctype=\"multipart/form-data\">\n<p>";

	for($i=0;$i<$number_of_uploads;$i++)
	{
	print "<input type=\"file\" name=\"file[]\" /><br />";
	}

print "<input type=\"hidden\" name=\"upload\" value=\"1\">\n";
print "<input type=\"submit\" value=\"Upload\"/></p>\n</form>\n";
}

$fileNAMES = array();

if(isset($_POST['upload']))
{
	for($i=0;$i<$number_of_uploads;$i++)
	{
		if(strlen($_FILES['file']['name'][$i]) > 0)
		{
		$ext = end(explode(".", $_FILES['file']['name'][$i]));

			if($rename_files == 1)
			{
			$fileNAMES[$i] = time()+rand(0,999999);
			}
			else
			{
			$xperiods = str_replace("." . $ext, "", $_FILES['file']['name'][$i]);
			$fileNAMES[$i] = str_replace(".", "", $xperiods);
			}

			if(!in_array(strtolower($ext), $allowed_file_types))
			{
			print "<p>FAILED: ". $_FILES['file']['name'][$i] ." ERROR: File type not allowed.</p>\n";
			}
			elseif($_FILES['file']['size'][$i] > ($max_size_in_kb*1024))
			{
			print "<p>FAILED: ". $_FILES['file']['name'][$i] ." ERROR: File size to large.</p>\n";
			}
			elseif(file_exists($upload_folder.$fileNAMES[$i] .".". $ext))
			{
			print "<p>FAILED: ". $fileNAMES[$i] .".". $ext ." ERROR: File already exists.</p>\n";
			}
			else
			{
				if(move_uploaded_file($_FILES['file']['tmp_name'][$i], $upload_folder.$fileNAMES[$i] .".". $ext))
				{
				print "<p>UPLOADED: ". $fileNAMES[$i] .".". $ext ."</p>\n";
				}
				else
				{
				print "<p>FAILED: ". $_FILES['file']['name'][$i] ." ERROR: Undetermined.</p>\n";
				}
			}
		}
	}

printForm();
}
else
{
printForm();
}

?>

</body>
</html>
Return current item: Basic Uploader