SEARCH
  Web Programming PHP Advanced File Uploader

Advanced File Uploader

Spacetech PHP Mar 19, 2007

Advanced File Uploader1. This allows users to upload files to your server in a specific folder, with a specific file size, with a specific allowed extension list, and more....

2. Copy/paste this code on the page you want

People to go to in order to upload files...this is a PHP FILE............lets say "upload.php"

<?php

//-------------------------
// Timer
//-------------------------
$mic_time = explode(" ",microtime());
$mic_time = $mic_time[1] + $mic_time[0];
$starttime = $mic_time;


//-------------------------
// Get info stuff
//-------------------------
require("config.php");


//-------------------------
// Show and make the page!
//-------------------------

echo "<title>$upload_name</title>";

if (!is_dir("$upload_dir"))
{
die ("Error: The Directory <b>$upload_dir</b> Doesn't Exist");
}
if (!is_writeable("$upload_dir"))
{
die ("Error: The Directory <b>$upload_dir</b> is NOT Writable... Please CHMOD to 777");
}

if (isset($_POST['upload_form']))
{
echo "<h3>$upload_name</h3>";

echo "<h3>Upload results:</h3>";


$new_file = $_FILES['file'];
$file_name = $new_file['name'];
$file_name = str_replace(' ', '_', $file_name);
$file_tmp = $new_file['tmp_name'];
$file_size = $new_file['size'];


if (!is_uploaded_file($file_tmp))
{
echo "File: Not selected.<br>";
}
else
{
$ext = strrchr($file_name,'.');
if (!in_array(strtolower($ext),$limitedext))
{
echo "File: ($file_name) Wrong File Extension. <br>";
}
else
{
if ($file_size > $size_bytes)
{
echo "File: ($file_name) Failed to Upload. File Must be <b>". $size_bytes / 1024 ."</b> KB. <br>";
}
else
{
if(file_exists($upload_dir.$file_name))
{
echo "File: ($file_name) Exists on Server.<br>";
}
else
{
if (move_uploaded_file($file_tmp,$upload_dir.$file_name))
{
echo "File: ".$file_array['name']." Uploaded.<br>";
echo "» <a href=$upload_dir$file_name>Go to File</a><br>";
}
else
{
echo "File $i: ($file_name) Failed to Upload.<br>";

} // End of (move_uploaded_file).

} // End of (file_exists).

} // End of (file_size).

} // End of (limitedext).

} // End of (!is_uploaded_file).

// End of (for loop).

echo "<br>« <a href=$_SERVER[PHP_SELF]>Go Back</a>";
echo "<br>« <a href=$upload_dir>Go to Uploaded Files Directory</a><br><br>";
}
else
{
echo "<h3>$upload_name</h3>";

echo "<h3>Select files to upload!</h3>
Max file size = ". $size_bytes / 1024 ." KB";
echo " <form method="post" action="$_SERVER[PHP_SELF]" enctype="multipart/form-data">";
echo "File: <input type="file" name="file" size="20">";
echo "<input type="hidden" name="MAX_FILE_SIZE" value="$size_bytes">
<input type="submit" name="upload_form" value="Upload Now">
</form>";
}

?>

<html>
<head>
<link rel="stylesheet" href="images/style.css" />
</head>
<body>

</body>
</html>

<?php

//-------------------------
// The REAL Timer
//-------------------------
$places = 5; // However many decimal places you want
$mic_time = explode(" ",microtime());
$mic_time = $mic_time[1] + $mic_time[0];
$finishtime = $mic_time;
echo "<br><br><br><center>Page loaded in ". round(($finishtime - $starttime),$places) ." secs</center";

?>

3. Now with

//-------------------------
// Get info stuff
//-------------------------
require("config.php");

Make a new file called "config.php" in the same directory as "upload.php" and copy/paste this into it:

<?php

$upload_name = "File Uploader"; //The name of your uploader
$upload_dir = "uploads/"; //<---Directory where the uploaded files go
$num_files = "5"; //<---Don't Touch It
$size_bytes = "1024005454"; //<---Max File Upload Size...in bytes
$limitedext = array(".gif",".jpg",".jpeg",".png",".txt",".nfo",".doc",".rtf",".htm",".dmg",".zip",".rar",".gz",".exe",".wav",".mp3",".mpeg",".wmv"); //<---The Allowed File extensions for uploading

?>

This is pretty much jsut a really stripped version of my uploader.

subscribe to newsletter