PDA

View Full Version : Problem checking filesize in upload script


Eike
08-01-2003, 08:20 AM
I wrote a script for uploading files to a chosen folder. The script works...almost. There is only a problem when I try to upload files that are bigger dan 5mb (=limit) he always returns that it is an invalid file type instead of saying that my file is too big. Can anybody explain me what I am doing wrong please?



<?php
if(file_exists('../../private/open_db.inc') && is_readable('../../private/open_db.inc'))
include('../../private/open_db.inc');

$db = OpenDb() or die ("can not connect");

// Define some variables
$author = $_SESSION['user'];
$date = date("d/m/y");

$subject = $_POST['subject'];
$userfile = $_POST['userfile'];
$description = $_POST['description'];
$max_size = $_POST['MAX_FILE_SIZE'];

$filetype = $_FILES['userfile']['type'];
$filename = $_FILES['userfile']['name'];
$filesize = $_FILES['userfile']['size'];

$uploadfile = $_FILES['userfile']['tmp_name'];
$uploaddir = "/opt/www/warniere/web/chemical.while.be/members/files/";
$filetype_array = array("application/pdf", "application/msword", "application/vnd.ms-excel", "application/x-zip-compressed", "image/jpeg", "image/gif", "image/png");
$upload_errors = array("<strong>$filename was uploaded succesfully!</strong> You can upload another file if you like.", "<strong>The file you are trying to upload is too big!</strong> Only files less than 5 mb are allowed.",
"<strong>The file you are trying to upload is too big!</strong> Only files less than 5 mb are allowed.", "<strong>The file was only partially uploaded.</strong> Please try again", "<strong>You didn't select a file to upload!</strong> Please select a file.<br />");


// Choose the directory were the file will be placed
switch($subject)
{
case 'IAP': $subject_dir = "IAP/";
break;
case 'UO2': $subject_dir = "UO2/";
break;
}

$file_destination = $uploaddir.$subject_dir.$filename;
$file_link = "http://www.chemical.while.be/members/files/".$subject_dir.$filename;

// Put file
if(($subject == "none") || (!in_array($filetype,$filetype_array)) || is_file($file_destination) || ($filesize >= $max_size))
{
if($subject == "none"){
echo "<span class=\"warning\">You didn't choose a subject for the file!</span> Please choose one below.<br />";
}
if($filesize > $max_size){
echo "<span class=\"warning\">The file you are trying to upload is too big!</span> Only files less than 5 mb are allowed.";
}
if (!in_array($filetype,$filetype_array)){
echo "<span class=\"warning\">Invalid file type!</span> Either you didn't select a file or you tried uploading a file other than a PDF, Word or Excel document, JPEG or GIF image file or Zip file.<br />";
}
if(is_file($file_destination)){
echo "<span class=\"warning\">The file you are trying to upload already exists in the database.</span> Please rename your file and try again.<br />";
}
include 'add_file_form.php'; // Show the form again!
return false;
}

if (is_uploaded_file($uploadfile))
{
move_uploaded_file($uploadfile, $file_destination);

// Add file info to database
$sql = "INSERT INTO chemical_files (subject,filename,description,link,abs_link,author,date) VALUES ('$subject','$filename','$description','$file_link','$file_destination','$author','$date')";
$result = mysql_query($sql);
chmod($file_destination, 0755);

echo $upload_errors[$_FILES['userfile']['error']];
include 'add_file_form.php';
}
else
echo $upload_errors[$_FILES['userfile']['error']];

?>