klemme 3 Posting Whiz in Training

Hey!
I have this script for Uploading PDF's to the DB.

I tjecks if the document ends on .pdf - and excludes everything that doesnt - which is fine.

BUT I have set a size limit on 1 MB for uploading, and it seems to be working. BUT if I try to upload a PDF BIGGER than 1 MB, I have an error message ($pdf_error[3]) saying so - BUT I dont get tht error message displayed, I get another error message every single time: ($pdf_error[4]).

How come the validation works, and I CANT display the correct error message when it is set?

Is my order in the script messy?

Handling the errors array in a wrong way?
PHP processing code:

<?php
$pdf_errors = array();

if(isset($_POST['submit'])){	

if(empty($_POST['title'])){
	$pdf_errors[1] = '<small style="color:red;">Husk at indtaste en titel!</small>';	
}else{
	$t = mysqli_real_escape_string($connection, strip_tags($_POST['title']));	
	}
if(empty($_POST['description'])){
	$pdf_errors[2] = '<small style="color:red;">Husk at indtaste noget i description!</small>';	
}else {
	$d = mysqli_real_escape_string($connection, strip_tags($_POST['description']));	
	}
	
if(is_uploaded_file($_FILES['pdf']['tmp_name']) && ($_FILES['pdf']['error']) == UPLOAD_ERR_OK){
$file = $_FILES['pdf'];
}
$size = round($file['size']);
if($size > 1048576 ){// 1MB in BYTES
	$pdf_errors[3] = '<small style="color:red;">Filen var for stor - max størrelse er 1000K!</small>';	
[B]// THIS ERROR MESSAGES[3] IS NOT BEING REGISTERED, BUT CODE SEEMS TO WORK![/B]
		}	
if(($file['type']!= 'application/pdf') && (substr($file['name'],-4)!='.pdf'))
	{
	$pdf_errors[4] = '<small style="color:red;">Det skal være et PDF dokument, prøv igen!</small>';	
	}
}

if(isset($_POST['submit']) && empty($pdf_errors)){
	$destination = 'PDFS_DIR' .$tmp_name.'_tmp';
	$tmp_name = sha1($file['name'].uniqid('', true));
	
	move_uploaded_file($tmp_name, $destination);
	
	// MOVING THE FILE:
	if(move_uploaded_file($file['tmp_name'], $destination)){
		$_SESSION['pdf']['tmp_name'] = $tmp_name;
		$_SESSION['pdf']['size'] = $size;
		$_SESSION['pdf']['file_name'] = $file['name'];
			$upload_succes = '<h4>The file has been uploaded!</h4>';
			}else {
				trigger_error('<p>The file could not be moved!</p>');
				unlink($file['tmp_name']);	
			}				
			
// Add pdf to database:
$fn = mysqli_real_escape_string($connection, $_SESSION['pdf']['file_name']);
$tmp_name = mysqli_real_escape_string($connection, $_SESSION['pdf']['tmp_name']);
$size = mysqli_real_escape_string($connection, $_SESSION['pdf']['size']);
$query = "INSERT INTO pdfs(tmp_name,title,description,file_name,size)
VALUES
('$tmp_name','$t','$d','$fn','$size')";
$result = mysqli_query($connection, $query);

$succes = '<p>The file has been succesfully uploaded to the db!</p>';
}
?>
// THE FORM FURTHER DOWN THE PAGE WITH THE ERROR MSG THAT DOESNT DISPLAY:
<?php if(!empty($succes)){
echo $succes;
}?>
<form action="add_pdf_to.php" method="post" enctype="multipart/form-data" accept-charset="utf-8">
<input type="hidden" name="MAX_FILESIZE" value="1048576" />
<p><b>Title:</b></p>
<input type="text" name="title" id="title" />
<br />
<?php if(!empty($pdf_errors[1])){echo $pdf_errors[1];} ?>
<br /><br />
<p><b>Description:</b></p>
<textarea name="description" cols="40" rows="10"></textarea>
<br />
<?php if(!empty($pdf_errors[2])){echo $pdf_errors[2];} ?>
<br /><br />
<p><b>Vælg PDF:</b></p>
<input type="file" name="pdf" id="pdf" />
<small>PDF only, 1MB Limit!</small>			
<br />
<?php if(!empty($pdf_errors[3])){echo $pdf_errors[3];} ?> 
<?php // THIS MSG WONT SHOW UP ?>
<?php if(!empty($pdf_errors[4])){echo $pdf_errors[4];} ?> 
<?php // THIS MSG SHOWS UP NO MATTER WHAT ERROR ?>
<br />
<input type="submit" name="submit" id="submit" value="Add This PDF" />
</form>

Anyone?

I hope someone has got time to tjeck it out as it is urgent for me to get it working properly.

Regards, 
Klemme
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.