Get ready for BLACK FRIDAY shopping starting in

Forums

PHP

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>&lt;?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],"/."&gt;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 "&lt;script&gt;window.location.href='$url_this?message=deleted successfully'&lt;/script&gt;";
}
}
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'] &gt; $MAX_SIZE)
$message = "The file size is over 2MB.";
else
$message = do_upload($upload_dir, $upload_url);

print "&lt;script&gt;window.location.href='$url_this?message=$message'&lt;/script&gt;";
}
else if (!$_FILES['userfile']);
else
$message = "Invalid File Specified.";

/************************************************************
* List Files
************************************************************/
If ($filesize &gt;= 10485760) {

$handle=opendir($upload_dir);
$filelist = "";
while ($file = readdir($handle)) {
if(!is_dir($file) && !is_link($file)) {
$filelist .= "&lt;a href='$upload_dir$file'&gt;".$file."&lt;/a&gt;";

$filelist .= "&lt;sub&gt;&lt;small&gt;&lt;small&gt;&lt;font color=grey&gt; ".date("d-m H:i", filemtime($upload_dir.$file))
."&lt;/font&gt;&lt;/small&gt;&lt;/small&gt;&lt;/sub&gt;";
$filelist .="&lt;br&gt;";
}
}

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("&#8211;","_",$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&lt;br&gt;";
echo "You are currently out of space, please delete files to make room to upload&lt;br&gt;";
echo "Or you can buy more space!";

?&gt;


&lt;center&gt;
&lt;font color=red&gt;&lt;?=$_REQUEST[message]?&gt;&lt;/font&gt;
&lt;br&gt;
&lt;br&gt;Your File Uploads&lt;hr width=70%&gt;
&lt;?=$filelist?&gt;
&lt;hr width=70%&gt;
&lt;br&gt;

&lt;form action="delete.php" method="post"&gt;
Type Your File Name: &lt;input type="text" name="delete"/&gt;
&lt;input type="submit" value="Delete File"/&gt;
&lt;/form&gt;

&lt;/center&gt;

&lt;?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";

?&gt;</font id=code></pre id=code>

PHP CODE WITHOUT IF STATEMENT
<pre id=code><font face=courier size=2 id=code>&lt;?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],"/."&gt;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 "&lt;script&gt;window.location.href='$url_this?message=deleted successfully'&lt;/script&gt;";
}
}
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'] &gt; $MAX_SIZE)
$message = "The file size is over 2MB.";
else
$message = do_upload($upload_dir, $upload_url);

print "&lt;script&gt;window.location.href='$url_this?message=$message'&lt;/script&gt;";
}
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 .= "&lt;a href='$upload_dir$file'&gt;".$file."&lt;/a&gt;";

$filelist .= "&lt;sub&gt;&lt;small&gt;&lt;small&gt;&lt;font color=grey&gt; ".date("d-m H:i", filemtime($upload_dir.$file))
."&lt;/font&gt;&lt;/small&gt;&lt;/small&gt;&lt;/sub&gt;";
$filelist .="&lt;br&gt;";
}
}

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("&#8211;","_",$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;
}

?&gt;


&lt;center&gt;
&lt;font color=red&gt;&lt;?=$_REQUEST[message]?&gt;&lt;/font&gt;
&lt;br&gt;
&lt;br&gt;Your File Uploads&lt;hr width=70%&gt;
&lt;?=$filelist?&gt;
&lt;hr width=70%&gt;
&lt;br&gt;

&lt;form action="delete.php" method="post"&gt;
Type Your File Name: &lt;input type="text" name="delete"/&gt;
&lt;input type="submit" value="Delete File"/&gt;
&lt;/form&gt;

&lt;/center&gt;

&lt;?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";

?&gt;</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

Reply to this topic