PHP upload to database I finally got my upload form and code that uploads to db. I used tutorial from this site.

As I am php newbie, I want to know how secure this code is and if it is not secure at all (which I am almost sure of...) could someone tell me how to make it secure. This upload should be only for pdf, jpeg, jpg, doc files. Also this upload will be available to public.

&lt;form method="post" enctype="multipart/form-data"&gt;
&lt;table width="350" border="0" cellpadding="1"
cellspacing="1" class="box"&gt;
&lt;tr&gt;
&lt;td&gt;please select a file&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;input type="hidden" name="MAX_FILE_SIZE"
value="16000000"&gt;
&lt;input name="userfile" type="file" id="userfile"&gt; 
&lt;/td&gt;
&lt;td width="80"&gt;&lt;input name="upload"
type="submit" class="box" id="upload" value=" Upload "&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
&lt;?php
if(isset($_POST['upload'])&amp;&amp;$_FILES['userfile']['size']&gt;0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName  = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$fileType=(get_magic_quotes_gpc()==0 ? mysql_real_escape_string(
$_FILES['userfile']['type']) : mysql_real_escape_string(
stripslashes ($_FILES['userfile'])));
$fp      = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);
if(!get_magic_quotes_gpc())
{
    $fileName = addslashes($fileName);
}
$con = mysql_connect('localhost', 'root', 'root') or die(mysql_error());
$db = mysql_select_db('olearyinternational', $con);
if($db){
$query = "INSERT INTO upload (name, size, type, content ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content')";
mysql_query($query) or die('Error, query failed'); 
mysql_close();
echo "&lt;br&gt;File $fileName uploaded&lt;br&gt;";
}else { echo "file upload failed"; }
} 
?&gt;
Restrict file types:
As your concern is security, you should make sure that the file type is one of those you allow.
For that you must implement a file type checker, that validates file type is allowed, then you let the user upload the file. Learn how to do that here or at stackOverflow.
Upload Size  You may like to have max upload size, so users do not upload too large files. Find how to do that at phpmaster.
Check the referrer: Check to make sure that the information being sent to your script is from your website and not an outside source. While this information can be faked, it's still a good idea to check.
Rename files: You can rename the files that are uploaded. In doing so, check for double-barreld extensions like yourfile.php.gif and eliminate extensions you don't allow, or remove the file completely.
Change permissions: Change the permissions on the upload folder so that files within it are not executable. Your FTP program probably allows you to chmod right from it.
Login and Moderate: Making your users login might deter some deviant behavior. You can also take the time to moderate all file uploads before allowing them to become live on the web.
Keep away from root: If your document root is /var/www/html, create a directory /var/www/uploads and use it to store uploaded files. That way, an attacker will not be able to retrieve the file directly. This will allow you to provide fine grained access control. The file will not be parsed by the server's application language module but the source of the file will be streamed.
Malware Scan:
The extension is right, and you checked that the file is actually a valid JPEG file per it's header. However, it could still be a malicious JPEG using one of the many image parser bugs to exploit clients downloading the file. There is no great defense against this as far as I am aware. One possible work around is to "rebuild" the file. Convert the JPEG to a GIF and back to a JPEG. This will likely strip out any malicious feature. But this technique could expose your servers to just the same image parser bugs.


Here are some links to help,

http://software-security.sans.org/blog/2009/12/28/8-basic-rules-to-implement-secure-file-uploads/

http://webcheatsheet.com/PHP/file_upload.php