Hi everyone and thanks for reading. I'm having problems validating the file size of my uploads. With my current script, If I upload anything that is over 2Mb, I keep getting the error about file types rather than the ones about the file size. Even if I dont submit anything, I still get the one about the file type. Why are the other errors not showing too? They're separate if statements. It must be something to do with my syntax. I've listed the code below anyway:
<html>
<head>
<title>Upload File To File Server</title>
</head>
<body>
<?
// $uploadDir is relative to the form because if you use a slash at the start it doesn't work.
// $uploadDirMySQL will be for the HTML to call and it should work fine.
$uploadDir = '../../images/photos/';
$uploadDirMySQL = "/images/photos/";
if (isset($_POST['upload'])) {
if (($_FILES["userfile"]["type"] == "image/gif") || ($_FILES["userfile"]["type"] == "image/jpeg") || ($_FILES["userfile"]["type"] == "image/pjpeg") || ($_FILES["userfile"]["type"] == "image/png") && ($_FILES['userfile']['size'] > 0) && ($_FILES['userfile']['size'] < 2000000)) {
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
// the files will be saved in filePath
$filePath = $uploadDir . $fileName;
$filePathMySQL = $uploadDirMySQL . $fileName;
// move the files to the specified directory
// if the upload directory is not writable or
// something else went wrong $result will be false
$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}
include 'config.php';
include 'opendb.php';
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
$filePathMySQL = addslashes($filePathMySQL);
}
$query = "INSERT INTO upload2 (name, size, type, path ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$filePathMySQL')";
mysql_query($query) or die('Error, query failed : ' . mysql_error());
include 'closedb.php';
echo "<p>File uploaded successfully:<br />";
echo "Name: " . $_FILES["userfile"]["name"] . " <br />";
echo "Size: " . round(($_FILES["userfile"]["size"] / 1024)) . " KB<br />";
echo "Type: " . $_FILES["userfile"]["type"] . "</p>";
} else {
if ($_FILES["userfile"]["size"] = 0) {
echo "<p>There was a problem uploading your file. Either the file is broken or you did not ask to upload anything. The file size is apparently 0kb which is impossible.</p>";
}
if ($_FILES["userfile"]["size"] > 2000000) {
echo "<p>There was a problem uploading your file. The file is too large. The limit is 2MB and your file was over that. Try uploading a smaller file.</p>";
}
if (($_FILES["userfile"]["type"] != "image/gif") || ($_FILES["userfile"]["type"] != "image/jpeg") || ($_FILES["userfile"]["type"] != "image/pjeg") || ($_FILES["userfile"]["type"] != "image/png")) {
echo "<p>There was a problem uploading your file. The file type was not correct. You can only upload <strong>jpg</strong>, <strong>gif</strong> or <strong>png</strong>.</p>";
}
} // This one closes the main part of the script.
} // This one closes the top if statement which checks to see if the upload button was clicked or not.
?>
<form action="" method="post" enctype="multipart/form-data" name="uploadform">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
<tr>
<td width="246"><input type="hidden" name="MAX_FILE_SIZE" value="2000000"><input name="userfile" type="file" class="box" id="userfile">
</td>
<td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>
</form>
</body>
</html>
Thanks for reading!
Anthony