Uploading file to MySQL Database Using PHP

Hi

can anyone please help, i trying to upload .pdf and some word document to a database.

so far have managed to upload most files but some file will not upload.

does anyone know why?

have attached a copy of my upload.php as a text file

please help

thanks

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>Upload File To MySQL Database</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<link rel="stylesheet" type="text/css" href="../styleCMS.css"/>

<style type="text/css">

<!--
.box {
	margin-left: auto;
	margin-right: auto;
	font-family: Arial, Helvetica, sans-serif;
	font-size: 12px;
	border: 1px solid #000000;
}
-->

</style>
</head>

<body>

<h1>Add Uploads</h1>

<?
if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
		$fileName = $_FILES['userfile']['name'];
		$tmpName  = $_FILES['userfile']['tmp_name'];
		$fileSize = $_FILES['userfile']['size'];
		$fileType = $_FILES['userfile']['type'];
		
		$fp = fopen($tmpName, 'r');
		$content = fread($fp, $fileSize);
		$content = addslashes($content);
		fclose($fp);
		
		if(!get_magic_quotes_gpc())
		{
			$fileName = addslashes($fileName);
		}
		
		include("../config.inc.php");
		
		$query = "INSERT INTO upload (name, size, type, content ) ".
		         "VALUES ('$fileName', '$fileSize', '$fileType', '$content')";

		mysql_query($query) or die('Error, query failed');					
		
		echo "<p class='style2' align='center'>File '$fileName' uploaded</p>";
}		
?>

<form 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="10000000"><!--10mb file limit-->
	  	  <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>

Do not use addslashes() on data being inserted into a MySQL database. Use mysql_real_escape_string(), the function that uses MySQL’s internal escaping mechanism.

This is especially important for binary data where some byte sequences must be escaped for successful insertion.

Hi dan

I have changed the function but this still does not work.

some pdf files can be uploaded but other pdfs can not, i have put a copy of the changes, are they correct?

also the frontend of the website is:

http://www.contactproject.org.uk/forms.php

the file i am uploading will be “AGM 05-06 Part 02.pdf”

we are testing at the moment.

Thanks for all the help.

What happens when it “does not work”? Does the query execute or fail? Does anything get written to the database?

Hi Dan

It does not wirte anything to the databse, it gives the “Error, query failed”.

So I am thinking the the query is failing for some reason.

the table table structure is :

id, int(11), Null=no, auto_increment

name, varchar(30), Collation=latin1_swedish_ci, Null=no

type, varchar(30), Collation=latin1_swedish_ci, Null=no

size, int(11), Null=no, default=0

content, mediumblob, Attributes=Binary, Null=no

hope this helps.

Thanks for help.

What is the host server OS? Unix or Windows? (You need the ‘b’ operator for Windows in the fopen() function).

Is there any correlation between files that fail and file size?

Try to get more info on WHY the query failed …

mysql_query($query) or die('Error, query failed. Reason: '.mysql_error());

You could try serializing the data, putting in a text field, and unserializing it as you need to. It works well enough for me.

Hi siteguru

its gives the error:

“Error, query failed. Reason: Got a packet bigger than ‘max_allowed_packet’ bytes”

I am new to php and MySQL, how do change the ‘max_allowed_packet’?

Thanks for the help

Start up mysql with

-O max_allowed_packet=maxsize

Or modify /etc/my.cnf if you’re on *nix (Sorry, haven’t been on windows for quite some time, on idea how to set it there)

Hi

Do I need to contact my hosting company to this as I use phpMyAdmin with control panel.

Or

Is there a way to change this myself?

Thanks

Contact your hosting company. There is no way to change this yourself on shared hosting

Thanks for all the help people!

If i get any more problems I will get back to you.

Thanks again.