<?php
$base_directory = "uploaded-files";
$directory = $_REQUEST['directory'];
$upload_dir = "";
$file = "";
$dir = "";
$POST_MAX_SIZE = ini_get('post_max_size');
$mul = substr($POST_MAX_SIZE, -1);
$mul = ($mul == 'M' ? 1048576 : ($mul == 'K' ? 1024 : ($mul == 'G' ? 1073741824 : 1)));
// check if the size is not larger than the size of the php.ini file
if ($_SERVER['CONTENT_LENGTH'] > $mul*(int)$POST_MAX_SIZE && $POST_MAX_SIZE) {
error_log("Total upload size is larger than post_max_size directive in the " .
"php.ini file ($POST_MAX_SIZE)");
error_log("The php.ini file is the configuration file of PHP");
error_log("Please check and change the post_max_size, upload_max_filesize " .
"and memory_limit directives");
error_log("The maximum value of these directives is 1990M");
error_log("Increase also the max_execution_time and the max_input_time directives");
header("HTTP/1.0 500 Internal Server Error");
exit;
}
// check if $base_directory exist
if (!is_dir($base_directory)) {
error_log('The $base_directory \'' . $base_directory . "' does not exist");
error_log('Please check the $base_directory in the upload.php script');
header("HTTP/1.0 500 Internal Server Error");
exit;
}
// check if the $base_directory is writeable
if (!is_writeable($base_directory)) {
error_log("The base_directory '" . $base_directory . "' has no write permissions.");
header("HTTP/1.0 500 Internal Server Error");
exit;
}
determineUploadDirectory();
foreach ($_FILES["userfile"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["userfile"]["tmp_name"][$key];
$name = $_FILES["userfile"]["name"][$key];
$decoded_name = urldecode($name);
$pos = strrpos($decoded_name, "/");
if ($pos === false) {
$file = $decoded_name;
$path = $upload_dir;
} else {
$file = substr($decoded_name, $pos + 1);
$dir = substr($decoded_name, 0, $pos);
$path = $upload_dir . "/" . $dir;
}
mkdir_recursive($path);
if(!move_uploaded_file($tmp_name, $path . "/" . $file)) {
if (!is_dir($path)) {
error_log("File: '" . $file . "' cannot be saved because the directory '"
. $path . "' does not exist.");
} else if (!is_writeable($path)) {
error_log("File: '" . $file . "' cannot be saved because the directory '"
. $path . "' has no write permissions.");
}
}
} else {
switch ($error) {
case UPLOAD_ERR_INI_SIZE:
error_log("The uploaded file exceeds the upload_max_filesize directive ("
. ini_get("upload_max_filesize") . ") in php.ini.");
break;
case UPLOAD_ERR_FORM_SIZE:
error_log("The uploaded file exceeds the MAX_FILE_SIZE directive that"
. " was specified in the HTML form.");
break;
case UPLOAD_ERR_PARTIAL:
error_log("The uploaded file was only partially uploaded.");
break;
case UPLOAD_ERR_NO_FILE:
error_log("No file was uploaded.");
break;
case UPLOAD_ERR_NO_TMP_DIR:
error_log("Missing a temporary folder.");
break;
case UPLOAD_ERR_CANT_WRITE:
error_log("Failed to write file to disk");
break;
}
header("HTTP/1.0 500 Internal Server Error");
exit;
}
}
//-----------------------------------------------------------------------------
// FUNCTIONS
//-----------------------------------------------------------------------------
// Creates the uploaddirectory. The uploaddirectory is based on the
// base_directory and the directory. The directory is the unique
// directory for every user. You can specify this directory in the
// param tag of the applet tag. See http://www.javaatwork.com/parameters.html
// The base_directory is specified in this php script.
// This method ensures that there's only one slash between the directories.
// e.g. c:/temp/files instead of c:/temp//files
function determineUploadDirectory() {
global $base_directory, $directory, $upload_dir;
//remove the slash of base_directory
$len = strlen ($base_directory);
$charAt = $base_directory{$len -1};
if ($charAt == '/') {
$base_directory = substr ($base_directory, 0, $len -1);
}
// remove the slashes from $directory
$charAt = $directory{0};
if ($charAt == '/') {
$directory = substr($directory, 1);
}
$len = strlen ($directory);
if ($len != 0) {
$charAt = $directory{$len - 1};
if ($charAt == '/') {
$directory = substr($directory, 0, $len -1);
}
$len = strlen ($directory );
$upload_dir = $base_directory . "/" . $directory;
} else {
$upload_dir = $base_directory;
}
}
// Creates the directories if needed.
function mkdir_recursive($dir){
do {
$tempdir = $dir;
while (!is_dir($tempdir)) {
$basedir = dirname($tempdir);
if ($basedir == '/' || is_dir($basedir)) {
mkdir($tempdir,0757);
// sometimes the chmod in the mkdir method doesn't work
chmod($tempdir, 0757);
} else {
$tempdir=$basedir;
}
}
} while ($tempdir != $dir);
}
?>
|