PDA

View Full Version : checking file size not working


Vman
06-22-2005, 05:05 AM
Hello everyone,
I got a little problem that I can't seem to work out. The script works fine locally, but not when it's uploaded. Here's the script:

<?php
$desc = $_POST['description'];
$project = $_POST['proj_id'];
$file_nm = $_FILES['file']['name'];
$file_name = basename($_FILES['file']['name']);
$temp = $_GET['temp'];
$photo_name=$_GET['photo_name'];
$client_name = $_POST['client_name'];
$typ = $_FILES['file']['type'];
$siz = $_FILES['file']['size'];
$max_file_size=3000000;
$path = 'content/'.$_POST['proj_id'].'/files/'.$file_nm;
$ext = ereg( ".([^\.]+)$", $file_name, $r ) ? $r[1] : "";
$uploaddir = 'content/'.$_POST['proj_id'].'/files';
//check to see if the file being uploaded exceeds the allowable file size ($max_file_size);
if ($siz > $max_file_size) {
//go to file_add.php and display the message if the file is too large;
$msg = "Unable to complete file upload. file must be <font color='000000'>".($max_file_size/1000000)."MB</font> or less.</br> $file_nm = <font color='000000'>".round($siz/1000000,2)."MB</font>";
$GoTo = "file_add.php?project_id=".$project."&client_name=".$client_name."&badfile=yes&message=".$msg;

header(sprintf("Location: %s", $GoTo));
exit();
}

//check to see if the name of the file being uploaded already exists;
else if (file_exists($path)) {
//get a random number to append to an existing file for re-naming purposes;
$rn = rand();
//if it does, rename the existing file by appending "$rn" to the file name;
rename('content/'.$_POST['proj_id'].'/files/'.$file_nm, 'content/'.$_POST['proj_id'].'/files/'.basename($file_nm, ".".$ext."")."_".$rn.".".$ext);
//set a variable with the new file name to update the database to the new name;
$file_rn = basename($file_nm, ".".$ext."")."_".$rn.".".$ext;
//the message that will be dsplayed on file_list.php letting the user know what just happened;
$msg = "The file you uploaded had the same name as an existing file. The previous file was renamed to <a href=./content/".$_POST['proj_id']."/files/".$file_rn." target=_blank>$file_rn</a>";
//update the database with the new file name of the existing file;
$updateSQL = "UPDATE files SET file_name='$file_rn' WHERE file_name='$file_nm' AND description NOT LIKE '$desc'";
//upload the new file;
if(is_uploaded_file($_FILES['file']['tmp_name']))
{
move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'.$_FILES['file']['name']);
}
//add the info to the database;
$insertSQL = "INSERT INTO files (project_id, description, file_name, file_date) VALUES ('$project', '$desc', '$file_name', NOW())";

mysql_select_db($database_ws, $ws);
$Result1 = mysql_query($insertSQL, $ws) or die(mysql_error());

mysql_select_db($database_ws, $ws);
$Result1 = mysql_query($updateSQL, $ws) or die(mysql_error());
$GoTo = "file_list.php?project_id=".$project."&client_name=".$client_name."&exists=yes&message=".$msg;

header(sprintf("Location: %s", $GoTo));
} else {
//if the file isn't too big and the name of the file being uploaded doesn't exist, just upload as usual;
if(is_uploaded_file($_FILES['file']['tmp_name']))
{
move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'.$_FILES['file']['name']);
}
$filename = $_FILES['file']['name'];
$insertSQL = "INSERT INTO files (project_id, description, file_name, file_date) VALUES ('$project', '$desc', '$filename', NOW())";
mysql_select_db($database_ws, $ws);
$Result1 = mysql_query($insertSQL, $ws) or die(mysql_error());
$insertGoTo = "file_list.php?project_id=".$project."&client_name=".$client_name;

header(sprintf("Location: %s", $insertGoTo));

mysql_close();
}
?>


I've also got this on the file_add.php page:
<input type="hidden" name="MAX_FILE_SIZE" value="3000000" />

At home it works like it should. It goes back to the page it started and dislplays the error message and nothing else.

If I test it online, I wait forever and it inserts the record in the database (which it should NOT) but does not upload the file. So I'll have a link to a file that doesn't exist. It should never insert the record in the database.

Any ideas?

BTW, is there anything out there that can check the size of a file BEFORE it starts uploading?

Thanks in advance!

Vman
06-30-2005, 09:15 PM
Nevermind.