Link to home
Start Free TrialLog in
Avatar of pingeyeg
pingeyeg

asked on

Getting friendly error message and can't figure out why

I have code that will enable a pdf document be uploaded to my client's website, but for some reason I keep getting my error friendly message even though the file size isn't too large.  Not sure what to make of it.  Below is my code:
if ($file['error'] == UPLOAD_ERR_OK) {
		// Now we are checking that the file uploaded was the right type.. we need
		// it to be a pdf.
		if ($file['type'] == 'application/pdf') {
			$target_path = '../pdf/';
			$target_path = $target_path . basename( $_FILES['strPDF']['name']); 
			copy($_FILES['strPDF']['tmp_name'], $target_path);
			
			if($target_path == "../pdf/") { 
				$action = "";
			} else {
				$action = basename($target_path);
			}
			
			$attach_sql = mysql_query("INSERT INTO `attachment` (`location`) VALUES ('$action')");
			$attach_id = mysql_insert_id(); // attachment_id of the last inserted attachment
				
			// We can now enter the stuff into the database..
			if (count($sections) > 0) {
				foreach($sections as $section) {
					$insertPDF = "INSERT INTO `attachments_to_sections` (`section_id`, `attachment_id`) values ('$section', '$attach_id')";	
					$result = mysql_query($insertPDF) or die(mysql_error());
				}
			}
		
			if(mysql_affected_rows() > 0) {
				$result = "Your PDF has been inserted into the specified sections.";		
			} else {
				$result = "Sorry but there was a complication in completing the task.  Please notify <a href=\"mailto:someone@domain.com\">Company</a> about the error.";
			}
		} else {
			$result = "The file you uploaded was not a pdf";	
		}
	} else {
		$result = "There was an error uploading your file";	
	}

Open in new window

Avatar of pingeyeg
pingeyeg

ASKER

Sorry, forgot to mention what the error message was:

"There was an error uploading your file"

This is based off of the first line:

if ($file['error'] == UPLOAD_ERR_OK) {
I would suggest checking to see what  $file['error']  is set to.

echo $file['error'];

Is this variable being set properly earrlier in your script?
Avatar of Zyloch
What is $file? Is it an alias to $_FILES['strPDF']?
Here is the full php code:

if($_POST['image_click']) {
 
	$file = $_FILES['strPDF'];
	$sections = $_POST['sections'];
	
	// We are just checking that everything was uploaded fine here. We could take
	// it further and check to see if the file was partial uploaded, etc to give
	// a more detailed error message.
	if ($file['error'] == UPLOAD_ERR_OK) {
		// Now we are checking that the file uploaded was the right type.. we need
		// it to be a pdf.
		if ($file['type'] == 'application/pdf') {
			$target_path = '../pdf/';
			$target_path = $target_path . basename( $_FILES['strPDF']['name']); 
			copy($_FILES['strPDF']['tmp_name'], $target_path);
			
			if($target_path == "../pdf/") { 
				$action = "";
			} else {
				$action = basename($target_path);
			}
			
			$attach_sql = mysql_query("INSERT INTO `attachment` (`location`) VALUES ('$action')");
			$attach_id = mysql_insert_id(); // attachment_id of the last inserted attachment
				
			// We can now enter the stuff into the database..
			if (count($sections) > 0) {
				foreach($sections as $section) {
					$insertPDF = "INSERT INTO `attachments_to_sections` (`section_id`, `attachment_id`) values ('$section', '$attach_id')";	
					$result = mysql_query($insertPDF) or die(mysql_error());
				}
			}
		
			if(mysql_affected_rows() > 0) {
				$result = "Your PDF has been inserted into the specified sections.";		
			} else {
				$result = "Sorry but there was a complication in completing the task.  Please notify <a href=\"mailto:username@domain.com\">Company</a> about the error.";
			}
		} else {
			$result = "The file you uploaded was not a pdf";	
		}
	} else {
		$result = "There was an error uploading your file";	
	}
}

Open in new window

Are you able to add this to the top of your code. This will let us see what the variable is being set to.

echo $_FILES['userfile']['error'] ;
I'll try that again :)

echo FILES['strPDF']['error'];
Sorry, I am going to put this in the code box.  Please ignore the echo code in my 2 posts immediately above.
echo $_FILES['strPDF']['error'];

Open in new window

The new EE input box parses some weird stuff :-p
The answer I get is 6.
ASKER CERTIFIED SOLUTION
Avatar of Zyloch
Zyloch
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Heh heh.  What Zyloch said 5 minutes before my post :)  I must be a slow typer.

Also, I have a gut feeling that ini_set() won't work in this case. It has to be the "real" php.ini file.