|
|
|
|
|
|
| |
File Uploading Class with JSON works as follows:
1. It can validate a user before the file is being uploded to the server.
2. It can easily validate the size of the file provided with the permitted file size.
3. It can test whether the file is alredy being uploaded or not?
4. It can check strong file extension matching provided by the programmer as an array. So that the user can't upload anonymous file.
5. It also writes a log file in the server about the history of the file being uploaded.
upload_class.php
| <?php
class Upload_Files {
var $temp_file_name;
var $file_name;
var $upload_dir;
var $upload_log_dir;
var $max_file_size;
var $banned_array;
var $ext_array;
/*
* purpose: To validate permitted extensions
* @param none
* @reutrn boolean
*/
function validate_extension() {
$file_name = trim($this->file_name);
$extension = strtolower(strrchr($file_name,"."));
$ext_array = $this->ext_array;
$ext_count = count($ext_array);
if (!$file_name)
{
return false;
}
else
{
if (!$ext_array)
{
return true;
}
else
{
foreach ($ext_array as $value)
{
$first_char = substr($value,0,1);
if ($first_char <> ".")
{
$extensions[] = ".".strtolower($value);
}
else
{
$extensions[] = strtolower($value);
}
}
foreach ($extensions as $value)
{
if ($value == $extension)
{
$valid_extension = "TRUE";
}
}
if ($valid_extension)
{
return true;
}
else
{
return false;
}
}
}
}
/*
* purpose: To validate file size
* @param none
* @return boolean
*/
function validate_size() {
$temp_file_name = trim($this->temp_file_name);
$max_file_size = trim($this->max_file_size);
if (!empty($temp_file_name))
{
$size = filesize($temp_file_name);
if ($size > $max_file_size)
{
return false;
}
else
{
return true;
}
}
else
{
return false;
}
}
/*
* purpose: Check if the file already exists or not
* @param none
* @return boolean
*/
function existing_file() {
$file_name = trim($this->file_name);
$upload_dir = $this->get_upload_directory();
if ($upload_dir == "ERROR")
{
return true;
}
else
{
$file = $upload_dir . $file_name;
if (file_exists($file))
{
return true;
}
else
{
return false;
}
}
}
/*
* purpose: Gets the original file size
* @param none
* @return file size
*/
function get_file_size() {
$temp_file_name = trim($this->temp_file_name);
$kb = 1024;
$mb = 1024 * $kb;
$gb = 1024 * $mb;
$tb = 1024 * $gb;
if ($temp_file_name)
{
$size = filesize($temp_file_name);
if ($size < $kb)
{
$file_size = "$size Bytes";
}
elseif ($size < $mb)
{
$final = round($size/$kb,2);
$file_size = "$final KB";
}
elseif ($size < $gb)
{
$final = round($size/$mb,2);
$file_size = "$final MB";
}
elseif($size < $tb)
{
$final = round($size/$gb,2);
$file_size = "$final GB";
}
else
{
$final = round($size/$tb,2);
$file_size = "$final TB";
}
}
else
{
$file_size = "ERROR: NO FILE PASSED TO get_file_size()";
}
return $file_size;
}
/*
* purpose: Gets the maximum file size allowed by the script
* @param none
* @return maximum file size
*/
function get_max_size() {
$max_file_size = trim($this->max_file_size);
$kb = 1024;
$mb = 1024 * $kb;
$gb = 1024 * $mb;
$tb = 1024 * $gb;
if ($max_file_size)
{
if ($max_file_size < $kb)
{
$max_file_size = "max_file_size Bytes";
}
elseif ($max_file_size < $mb)
{
$final = round($max_file_size/$kb,2);
$max_file_size = "$final KB";
}
elseif ($max_file_size < $gb)
{
$final = round($max_file_size/$mb,2);
$max_file_size = "$final MB";
}
elseif($max_file_size < $tb)
{
$final = round($max_file_size/$gb,2);
$max_file_size = "$final GB";
}
else
{
$final = round($max_file_size/$tb,2);
$max_file_size = "$final TB";
}
}
else
{
$max_file_size = "ERROR: NO SIZE PARAMETER PASSED TO get_max_size()";
}
return $max_file_size;
}
/*
* purpose: Check if the user is banned or nor
* @param none
* @return boolean
*/
function validate_user() {
$banned_array = $this->banned_array;
$ip = trim($_SERVER['REMOTE_ADDR']);
$cpu = gethostbyaddr($ip);
$count = count($banned_array);
if ($count < 1)
{
return true;
}
else
{
foreach($banned_array as $key => $value)
{
if ($value == $ip ."-". $cpu)
{
return false;
}
else
{
return true;
}
}
}
}
/*
* purpose: Gets the upload directory
* @param none
* @return upload directory
*/
function get_upload_directory() {
$upload_dir = trim($this->upload_dir);
if ($upload_dir)
{
$ud_len = strlen($upload_dir);
$last_slash = substr($upload_dir,$ud_len-1,1);
if ($last_slash <> "/")
{
$upload_dir = $upload_dir."/";
}
else
{
$upload_dir = $upload_dir;
}
$handle = @opendir($upload_dir);
if ($handle)
{
$upload_dir = $upload_dir;
closedir($handle);
}
else
{
$upload_dir = "ERROR";
}
}
else
{
$upload_dir = "ERROR";
}
return $upload_dir;
}
/*
* purpose: Gets the upload logs directory
* @param none
* @return upload log directory
*/
function get_upload_log_directory() {
$upload_log_dir = trim($this->upload_log_dir);
if ($upload_log_dir)
{
$ud_len = strlen($upload_log_dir);
$last_slash = substr($upload_log_dir,$ud_len-1,1);
if ($last_slash <> "/")
{
$upload_log_dir = $upload_log_dir."/";
}
else
{
$upload_log_dir = $upload_log_dir;
}
$handle = @opendir($upload_log_dir);
if ($handle)
{
$upload_log_dir = $upload_log_dir;
closedir($handle);
}
else
{
$upload_log_dir = "ERROR";
}
}
else
{
$upload_log_dir = "ERROR";
}
return $upload_log_dir;
}
/*
* purpose: Upload a file without validation
* @param none
* @return boolean
*/
function upload_file_no_validation() {
$temp_file_name = trim($this->temp_file_name);
$file_name = trim(strtolower($this->file_name));
$upload_dir = $this->get_upload_directory();
$upload_log_dir = $this->get_upload_log_directory();
$file_size = $this->get_file_size();
$ip = trim($_SERVER['REMOTE_ADDR']);
$cpu = gethostbyaddr($ip);
$m = date("m");
$d = date("d");
$y = date("Y");
$date = date("m/d/Y");
$time = date("h:i:s A");
if (($upload_dir == "ERROR") OR ($upload_log_dir == "ERROR"))
{
return false;
}
else
{
if (is_uploaded_file($temp_file_name))
{
if (move_uploaded_file($temp_file_name,$upload_dir . $file_name))
{
$log = $upload_log_dir.$y."_".$m."_".$d.".txt";
$fp = fopen($log,"a+");
fwrite($fp,"$ip-$cpu | $file_name | $file_size | $date | $time");
fclose($fp);
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
}
/*
* purpose: Upload a file with validation
* @param none
* @return boolean
*/
function upload_file_with_validation() {
$temp_file_name = trim($this->temp_file_name);
$file_name = trim(strtolower($this->file_name));
$upload_dir = $this->get_upload_directory();
$upload_log_dir = $this->get_upload_log_directory();
$file_size = $this->get_file_size();
$ip = trim($_SERVER['REMOTE_ADDR']);
$cpu = gethostbyaddr($ip);
$m = date("m");
$d = date("d");
$y = date("Y");
$date = date("m/d/Y");
$time = date("h:i:s A");
$existing_file = $this->existing_file();
$valid_user = $this->validate_user();
$valid_size = $this->validate_size();
$valid_ext = $this->validate_extension();
if (($upload_dir == "ERROR") OR ($upload_log_dir == "ERROR"))
{
return false;
}
elseif ((((!$valid_user) OR (!$valid_size) OR (!$valid_ext) OR ($existing_file))))
{
return false;
}
else
{
if (is_uploaded_file($temp_file_name))
{
if (move_uploaded_file($temp_file_name,$upload_dir . $file_name))
{
$log = $upload_log_dir.$y."_".$m."_".$d.".txt";
$fp = fopen($log,"a+");
fwrite($fp,"$ip-$cpu | $file_name | $file_size | $date | $time\n");
fclose($fp);
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
}
}
?> | |
upload.php
| <html>
<head>
<title>Upload</title>
<link rel="stylesheet" href="upload.css" type="text/css">
<script language="JavaScript" src="json/json.js"></script>
<script language="JavaScript">
var httpSubmit = getHTTPObject();
/*
* Sends a request to the server
* @param none
* @return none
*/
function getFileInfo()
{
var uploaded = new Uploaded();
uploaded.filesName = '';
var json_text = uploaded.toJSONString();
httpSubmit.open("POST", "json_upload.php" , true);
httpSubmit.onreadystatechange = handleHttpSubmitResponse;
httpSubmit.send(json_text);
}
function Uploaded()
{
this.filesName = "";
}
/*
* Handles response sent from the server
* @param none
* @return none
*/
function handleHttpSubmitResponse()
{
if (httpSubmit.readyState == 4)
{
var uploaded = httpSubmit.responseText.parseJSON();
if(uploaded.filesName == undefined)
{
document.getElementById("showFile").innerHTML = "File not uploaded!";
}
else
{
document.getElementById("showFile").innerHTML = uploaded.filesName;
}
document.getElementById("showFile").style.display = "block";
}
}
function getHTTPObject()
{
var xmlhttp;
if (!xmlhttp)
{
if(window.XMLHttpRequest)
{
try
{
xmlhttp = new XMLHttpRequest();
}
catch(e)
{
xmlhttp = false;
}
}
else if(window.ActiveXObject) // branch for IE/Windows ActiveX version
{
try
{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
xmlhttp = false;
}
}
}
return xmlhttp;
}
}
window.onload = function()
{
setInterval("getFileInfo()",1000);
}
</script>
</head>
<body>
<iframe src="upload.html" style="border-width:1; border-style:solid; border-color:000000; width:300; height:70;">
</iframe><br>
<div class="style1" id="showFile" name="showFile" style="display: none; padding:8px 8px 8px 8px;"></div>
</body>
</html> | |
upload.html
| <html>
<head>
<link rel="stylesheet" href="upload.css" type="text/css">
<script language="JavaScript">
function autoFormSubmit()
{
document.getElementById("upload_form").submit();
}
</script>
</head>
<body>
<form class='style1' name="upload_form" id="upload_form" enctype="multipart/form-data" method="POST" action="upload1.php" onSubmit="return false;">
<input type="hidden" name="MAX_FILE_SIZE" value="100000000" />
Send this file: <input name="userfile" id="userfile" type="file" onChange="autoFormSubmit();"/>
<div id="txt" style="display: none"></div>
</form>
</body>
</html> | | |
|
| file class , uploade file , download file already uploaded on another website Categories : PHP, PHP Classes, Filesystem, Web Services | | | Zephyr: AJAX Based Framework for PHP5 Developers Categories : PHP, AJAX, Frameworks, Java Script, Web Applications | | | Introduction to Language Files Categories : PHP, Filesystem, Beginner Guides | | | Random Image Display Categories : PHP, Filesystem, Graphics, HTML and PHP | | | The toll booth Categories : PHP, Java Script, Filesystem | | | php jump urls...the best way Categories : PHP, URLs, Filesystem | | | Simple way to replace a variable value in a .conf (.ini) file using a
webbrowser - the first stage of a complete universal configuration editor Categories : PHP, Regexps, Code Editors, Filesystem | | | Display list of files within current and subdirectories (recursively) showing
each file as an anchored link and each directory as a category header. Categories : Filesystem, Directories, Arrays, PHP | | | File Explorer, browse, upload, download and edit your web site files with only a browser and a HTTP connection. Categories : Complete Programs, Content Management, Filesystem, PHP | | | GuestBook Light - a plug and play application for any website. Categories : PHP, Complete Programs, Filesystem, Sessions | | | The Ajax Tree view class fetches data from a db for the requested parent category id. The data is then stored in an array and converted into JSON (Javascript Object Notation) format. This format is then used by JavaScript for populating tree view. Categories : PHP, PHP Classes, Java Script, AJAX, Databases | | | Remote Scripting: send form POST data to a script and insert the results into a page without refreshing the page. Categories : PHP, AJAX, HTML and PHP, Java Script | | | Simple pipe delimited file export program that downloads to a local machine Categories : PHP, Filesystem, Databases, MySQL, HTTP | | | Recursive function to move files on a filesystem. It can be minor changed in order to copy recursively.
Categories : PHP, Filesystem, Algorithms | | | Extended Get File List Function Categories : PHP, Filesystem, Search, Directories | |
|
|
|