We help IT Professionals succeed at work.

Getting friendly error message and can't figure out why

pingeyeg
pingeyeg asked
on
167 Views
Last Modified: 2008-07-14
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

Comment
Watch Question

Author

Commented:
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?
CERTIFIED EXPERT
Top Expert 2004

Commented:
What is $file? Is it an alias to $_FILES['strPDF']?

Author

Commented:
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

CERTIFIED EXPERT
Top Expert 2004

Commented:
The new EE input box parses some weird stuff :-p

Author

Commented:
The answer I get is 6.
CERTIFIED EXPERT
Top Expert 2004
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
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.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.