Forums
This topic is locked
Blocking upload if folder size to big
Posted 22 Feb 2008 07:58:03
1
has voted
22 Feb 2008 07:58:03 landin martens posted:
Hey guys, first post, but i'm having troubles.Basically you upload files, on the site im making and if u are = or over X then u cant upload any more. But i keep getting errors on my If statement i think im doing a really noobish move but guys what can ya see and what should i fix?
PHP CODE WITH IF STATEMENT TO BLOCK UPLOADS
<pre id=code><font face=courier size=2 id=code><?php
function GetFolderSize($d ="." ) {
$h = @opendir($d);
if($h==0)return 0;
while ($f=readdir($h)){
if ( $f!= ".." {
$sf+=filesize($nd=$d."/".$f);
if($f!="."&&is_dir($nd)){
$sf+=GetFolderSize ($nd);
}
}
}
closedir($h);
return $sf ;
}
$path ="files/";
//* Here I change the size of the file from bytes to a variable called $foldersize
$foldersize ="" . GetFolderSize( $path ) . "";
//* Here I make the size of 1 Gb in bytes to a variable called $gbsize so I can then divide the size in bytes by the size of a
//*Gigabyte
$gbsize = 1048576;
//* Here I am combining the two variables and making then divide so I will be left with the size of the directory in Gb
$filesize = $foldersize/$gbsize;
//Maximum file size. You may increase or decrease.
$MAX_SIZE = 20000000000000000000000000000000000000000000000000;
//Allowable file Mime Types. Add more mime types if you want
$FILE_MIMES = array('');
//Allowable file ext. names. you may add more extension names.
$FILE_EXTS = array('');
//Allow file delete? no, if only allow upload only
$DELETABLE = true;
//
// Do not touch the below if you are not confident.
//
/************************************************************
* Setup variables
************************************************************/
$site_name = $_SERVER['HTTP_HOST'];
$url_dir = "".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
$url_this = "".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
$upload_dir = "files/";
$upload_url = $url_dir."/files/";
$message ="";
/************************************************************
* Create Upload Directory
************************************************************/
if (!is_dir("files") {
if (!mkdir($upload_dir))
die ("upload_files directory doesn't exist and creation failed"
if (!chmod($upload_dir,0755))
die ("change permission to 755 failed."
}
/************************************************************
* Process User's Request
************************************************************/
if ($_REQUEST[del] && $DELETABLE) {
$resource = fopen("log.txt","a"
fwrite($resource,date("Ymd h:i:s"."DELETE - $_SERVER[REMOTE_ADDR]"."$_REQUEST[del]\n"
fclose($resource);
if (strpos($_REQUEST[del],"/.">0); //possible hacking
else if (strpos($_REQUEST[del],$upload_dir) === false); //possible hacking
else if (substr($_REQUEST[del],0,6)==$upload_dir) {
unlink($_REQUEST[del]);
print "<script>window.location.href='$url_this?message=deleted successfully'</script>";
}
}
else if ($_FILES['userfile']) {
$resource = fopen("log.txt","a"
fwrite($resource,date("Ymd h:i:s"."UPLOAD - $_SERVER[REMOTE_ADDR]"
.$_FILES['userfile']['name']." "
.$_FILES['userfile']['type']."\n"
fclose($resource);
$file_type = $_FILES['userfile']['type'];
$file_name = $_FILES['userfile']['name'];
$file_ext = strtolower(substr($file_name,strrpos($file_name,"."));
//File Size Check
if ( $_FILES['userfile']['size'] > $MAX_SIZE)
$message = "The file size is over 2MB.";
else
$message = do_upload($upload_dir, $upload_url);
print "<script>window.location.href='$url_this?message=$message'</script>";
}
else if (!$_FILES['userfile']);
else
$message = "Invalid File Specified.";
/************************************************************
* List Files
************************************************************/
If ($filesize >= 10485760) {
$handle=opendir($upload_dir);
$filelist = "";
while ($file = readdir($handle)) {
if(!is_dir($file) && !is_link($file)) {
$filelist .= "<a href='$upload_dir$file'>".$file."</a>";
$filelist .= "<sub><small><small><font color=grey> ".date("d-m H:i", filemtime($upload_dir.$file))
."</font></small></small></sub>";
$filelist .="<br>";
}
}
function do_upload($upload_dir, $upload_url) {
$temp_name = $_FILES['userfile']['tmp_name'];
$file_name = $_FILES['userfile']['name'];
$file_name = str_replace("\\","",$file_name);
$file_name = str_replace("'","",$file_name);
$file_name = str_replace("-","_",$file_name);
$file_name = str_replace("–","_",$file_name);
$file_path = $upload_dir.$file_name;
//File Name Check
if ( $file_name =="" {
$message = "Invalid File Name Specified";
return $message;
}
$result = move_uploaded_file($temp_name, $file_path);
if (!chmod($file_path,0777))
$message = "change permission to 777 failed.";
else
$message = ($result)?"$file_name uploaded successfully." :
"Somthing is wrong with uploading a file.";
return $message;
}
} else {
echo "You are currently Using: ";
printf ("%01.2f", $filesize);
echo " MegaBytes Space Out Of 10MBs<br>";
echo "You are currently out of space, please delete files to make room to upload<br>";
echo "Or you can buy more space!";
?>
<center>
<font color=red><?=$_REQUEST[message]?></font>
<br>
<br>Your File Uploads<hr width=70%>
<?=$filelist?>
<hr width=70%>
<br>
<form action="delete.php" method="post">
Type Your File Name: <input type="text" name="delete"/>
<input type="submit" value="Delete File"/>
</form>
</center>
<?php
function GetFolderSize($d ="." ) {
$h = @opendir($d);
if($h==0)return 0;
while ($f=readdir($h)){
if ( $f!= ".." {
$sf+=filesize($nd=$d."/".$f);
if($f!="."&&is_dir($nd)){
$sf+=GetFolderSize ($nd);
}
}
}
closedir($h);
return $sf ;
}
$path ="files/";
//* Here I change the size of the file from bytes to a variable called $foldersize
$foldersize ="" . GetFolderSize( $path ) . "";
//* Here I make the size of 1 Gb in bytes to a variable called $gbsize so I can then divide the size in bytes by the size of a
//*Gigabyte
$gbsize = 1048576;
//* Here I am combining the two variables and making then divide so I will be left with the size of the directory in Gb
$filesize = $foldersize/$gbsize;
//* This may look stupid but this is the only way I could show the directory size to 2 decimal places
echo "You are currently Using: ";
printf ("%01.2f", $filesize);
echo " MegaBytes Space Out Of 10MBs";
?></font id=code></pre id=code>
PHP CODE WITHOUT IF STATEMENT
<pre id=code><font face=courier size=2 id=code><?php
//Maximum file size. You may increase or decrease.
$MAX_SIZE = 20000000000000000000000000000000000000000000000000;
//Allowable file Mime Types. Add more mime types if you want
$FILE_MIMES = array('');
//Allowable file ext. names. you may add more extension names.
$FILE_EXTS = array('');
//Allow file delete? no, if only allow upload only
$DELETABLE = true;
//
// Do not touch the below if you are not confident.
//
/************************************************************
* Setup variables
************************************************************/
$site_name = $_SERVER['HTTP_HOST'];
$url_dir = "".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
$url_this = "".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
$upload_dir = "files/";
$upload_url = $url_dir."/files/";
$message ="";
/************************************************************
* Create Upload Directory
************************************************************/
if (!is_dir("files") {
if (!mkdir($upload_dir))
die ("upload_files directory doesn't exist and creation failed"
if (!chmod($upload_dir,0755))
die ("change permission to 755 failed."
}
/************************************************************
* Process User's Request
************************************************************/
if ($_REQUEST[del] && $DELETABLE) {
$resource = fopen("log.txt","a"
fwrite($resource,date("Ymd h:i:s"."DELETE - $_SERVER[REMOTE_ADDR]"."$_REQUEST[del]\n"
fclose($resource);
if (strpos($_REQUEST[del],"/.">0); //possible hacking
else if (strpos($_REQUEST[del],$upload_dir) === false); //possible hacking
else if (substr($_REQUEST[del],0,6)==$upload_dir) {
unlink($_REQUEST[del]);
print "<script>window.location.href='$url_this?message=deleted successfully'</script>";
}
}
else if ($_FILES['userfile']) {
$resource = fopen("log.txt","a"
fwrite($resource,date("Ymd h:i:s"."UPLOAD - $_SERVER[REMOTE_ADDR]"
.$_FILES['userfile']['name']." "
.$_FILES['userfile']['type']."\n"
fclose($resource);
$file_type = $_FILES['userfile']['type'];
$file_name = $_FILES['userfile']['name'];
$file_ext = strtolower(substr($file_name,strrpos($file_name,"."));
//File Size Check
if ( $_FILES['userfile']['size'] > $MAX_SIZE)
$message = "The file size is over 2MB.";
else
$message = do_upload($upload_dir, $upload_url);
print "<script>window.location.href='$url_this?message=$message'</script>";
}
else if (!$_FILES['userfile']);
else
$message = "Invalid File Specified.";
/************************************************************
* List Files
************************************************************/
$handle=opendir($upload_dir);
$filelist = "";
while ($file = readdir($handle)) {
if(!is_dir($file) && !is_link($file)) {
$filelist .= "<a href='$upload_dir$file'>".$file."</a>";
$filelist .= "<sub><small><small><font color=grey> ".date("d-m H:i", filemtime($upload_dir.$file))
."</font></small></small></sub>";
$filelist .="<br>";
}
}
function do_upload($upload_dir, $upload_url) {
$temp_name = $_FILES['userfile']['tmp_name'];
$file_name = $_FILES['userfile']['name'];
$file_name = str_replace("\\","",$file_name);
$file_name = str_replace("'","",$file_name);
$file_name = str_replace("-","_",$file_name);
$file_name = str_replace("–","_",$file_name);
$file_path = $upload_dir.$file_name;
//File Name Check
if ( $file_name =="" {
$message = "Invalid File Name Specified";
return $message;
}
$result = move_uploaded_file($temp_name, $file_path);
if (!chmod($file_path,0777))
$message = "change permission to 777 failed.";
else
$message = ($result)?"$file_name uploaded successfully." :
"Somthing is wrong with uploading a file.";
return $message;
}
?>
<center>
<font color=red><?=$_REQUEST[message]?></font>
<br>
<br>Your File Uploads<hr width=70%>
<?=$filelist?>
<hr width=70%>
<br>
<form action="delete.php" method="post">
Type Your File Name: <input type="text" name="delete"/>
<input type="submit" value="Delete File"/>
</form>
</center>
<?php
function GetFolderSize($d ="." ) {
$h = @opendir($d);
if($h==0)return 0;
while ($f=readdir($h)){
if ( $f!= ".." {
$sf+=filesize($nd=$d."/".$f);
if($f!="."&&is_dir($nd)){
$sf+=GetFolderSize ($nd);
}
}
}
closedir($h);
return $sf ;
}
$path ="files/";
//* Here I change the size of the file from bytes to a variable called $foldersize
$foldersize ="" . GetFolderSize( $path ) . "";
//* Here I make the size of 1 Gb in bytes to a variable called $gbsize so I can then divide the size in bytes by the size of a
//*Gigabyte
$gbsize = 1048576;
//* Here I am combining the two variables and making then divide so I will be left with the size of the directory in Gb
$filesize = $foldersize/$gbsize;
//* This may look stupid but this is the only way I could show the directory size to 2 decimal places
echo "You are currently Using: ";
printf ("%01.2f", $filesize);
echo " MegaBytes Space Out Of 10MBs";
?></font id=code></pre id=code>
Thanks for any help i get! thanks
Replies
Replied 29 Feb 2008 03:38:28
29 Feb 2008 03:38:28 Ryan G replied:
Wow, that's alot of code. I use a Dreamweaver extension for all my file uploads that includes the ability to set a maximum file size as well as file extension. You can read about it at www.justdreamweaver.com/webassist-digital-file-pro.html