rendered paste body<?php
ob_start();
/* upload.php
*
* Description: A little bit
* complex upload script, with
* different settings on how
* files are uploaded, etc.
*
* Author: eax
* Contact: eax@eax.gopzap.com
* Compiler: None, interpreted.
* Interpreter: PHP 5.1.2 (Running under a web server)
* Operative System: Any that supports
* PHP
*/
/*
** Configuration
*/
$conf['upload'] = true; /* True to allow, false to deny */
/*
** Protection cookie configuration
*/
$conf['cookie']['use'] = true; /* Use protection cookie */
$conf['cookie']['time'] = 1; /* Time in minutes */
$conf['cookie']['name'] = "eax_upload"; /* Cookie name */
/*
** Directories & Extensions
*/
$conf['dir']['name'] = "uploads"; /* Directory name to upload (without backslash at the end) */
$conf['dir']['encrypt'] = false; /* Encrypt file name using MD5 so there won't be equal names (true or false), if this option is false, the file will be renamed to its file number on the upload directory */
$conf['dir']['uhash'] = false; /* When encrypt is set to true, you can select on using md5 or just using the file name without the extension... */
$conf['dir']['formats'] = array('png','jpg','jpeg','gif','bmp'); /* Allowed extensions to be uploaded */
$conf['dir']['filesize'] = "2097152"; /* Max file size */
/*
** Logging and informating
*/
$conf['inf']['log'] = true; /* Log who uploads? (true or false) */
$conf['inf']['file'] = "upload.log"; /* File to log everyone who uploads a file */
/*
** Code starts!... Don't touch unless you know what you are doing!
*/
if(!function_exists('scandir')) {
function scandir($dir, $sortorder = 0) {
if(is_dir($dir) && $dirlist = @opendir($dir)) {
while(($file = readdir($dirlist)) !== false) {
$files[] = $file;
}
closedir($dirlist);
($sortorder == 0) ? asort($files) : rsort($files); // arsort was replaced with rsort
return $files;
} else return false;
}
}
if ($conf['upload'] != true) { die("Uploader is blocked from the configuration!."); }
if (!isset($_POST) || empty($_POST) || empty($_FILES['upload']['name'])) {
echo "<form method="POST" enctype="multipart/form-data" action="">n";
echo "<input type='hidden' name='max_file_size' value='{$conf['dir']['filesize']}'>n";
echo "File dir: <input type="file" name="upload"><br>n";
echo "<input type="submit" value="Upload" name="upload"><br>n";
echo "</form>n";
ob_end_flush();
}
elseif ($conf['cookie']['use'] == true && isset($_COOKIE[$conf['cookie']['name']])) {
echo "You already have uploaded a file in lower than ".$conf['cookie']['time']." minutes... Please wait!<br>n";
ob_end_flush();
}
else {
if (!is_dir($conf['dir']['name']) && !mkdir($conf['dir']['name'],0777)) {
echo "Couldn't upload file, the directory ".$conf['dir']['name']." doesn't exists!...<br>n";
ob_end_flush();
die();
}
// Retrieve the file extension...
$gext = explode(".",$_FILES['upload']['name']);
$ext = $gext[(count($gext)-1)];
$noext = str_replace(".".$ext,"",$_FILES['upload']['name']);
unset($gext);
if (!is_numeric(array_search($ext,$conf['dir']['formats']))) {
echo 'Only the following extensions are valid: ';
foreach ($conf['dir']['formats'] as $myval) { echo "<b>".$myval."<b> "; }
echo "<br>n";
ob_end_flush();
die();
}
if (!isset($conf['dir']['encrypt']) || $conf['dir']['encrypt'] == false) {
$listfiles = scandir($conf['dir']['name']);
if ($listfiles == false) {
echo "Couldn't list the upload directory!...<br>n";
ob_end_flush();
die();
}
$files = 0;
foreach ($listfiles as $val) { if ($val != "." && $val != ".." && !is_dir($val)) { $files++; } }
unset($listfiles);
$file['fullname'] = "{$files}.{$ext}";
$file['name'] = $files;
if (is_file($file['fullname'])) {
echo "Random error while renaming your file!...<br>n";
ob_end_flush();
die();
}
}
else {
if ($conf['dir']['uhash'] == true) { $mfile = md5($noext).rand(1,999999999999); }
else { $mfile = $noext.rand(1,999999999999); }
$file['fullname'] = $mfile.".".$ext;
$file['name'] = $mfile;
unset($mfile);
if (is_file($file['fullname'])) {
echo "Your file can't be renamed as a file with the same renamed name exists. Please try again!...<br>n";
ob_end_flush();
die();
}
}
$file['extension'] = $ext;
if ($_FILES['upload']['error'] != 0 && $_FILES['upload']['error'] != 2) {
echo "Random error while uploading your file!...<br>n";
ob_end_flush();
die();
}
elseif ($_FILES['upload']['error'] == 2) {
echo "Your file size is higher than ".round(($conf['dir']['filesize']/1048576),1)." MB (megabytes)...<br>n";
ob_end_flush();
die();
}
if (move_uploaded_file($_FILES['upload']['tmp_name'],"{$conf['dir']['name']}/{$file['fullname']}")) {
$buildurl = "{$conf['dir']['name']}/{$file['fullname']}";
echo "Your file has been successfully uploaded!. Click <a href="{$buildurl}">here</a> to see it!.<br>n";
if ($conf['cookie']['use'] == true) {
setcookie($conf['cookie']['name'],$file['fullname'],time()+($conf['cookie']['time']*60));
}
if ($conf['inf']['log'] == true) {
if (getenv('HTTP_CLIENT_IP')) { $ip = getenv('HTTP_CLIENT_IP'); }
elseif (getenv('HTTP_X_FORWARDED_FOR')) { $ip = getenv('HTTP_X_FORWARDED_FOR'); }
elseif (getenv('HTTP_X_FORWARDED')) { $ip = getenv('HTTP_X_FORWARDED'); }
elseif (getenv('HTTP_FORWARDED_FOR')) { $ip = getenv('HTTP_FORWARDED_FOR'); }
elseif (getenv('HTTP_FORWARDED')) { $ip = getenv('HTTP_FORWARDED'); }
else { $ip = $_SERVER['REMOTE_ADDR']; }
$fload = fopen($conf['inf']['file'],"a");
fwrite($fload,"=> Uploaded file {$file['fullname']} by user @ IP {$ip}n");
fclose($fload);
unset($fload,$ip);
}
ob_end_flush();
die();
}
else {
echo "Couldn't upload the file!...<br>n";
ob_end_flush();
die();
}
}
ob_end_flush();
?>