Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

mysql file upload



7:27 pm on Feb 19, 2008 (gmt 0)

5+ Year Member

Ok Im trying to upload files and have them stored in a table on mah database, but its not working out at all. I've been trying the whole day and can't get anything to work. Here is my code:

<h3>Please Choose a File and click Submit</h3>

<form enctype="multipart/form-data" action="" method="post">
<input name="userfile[]" type="file" />
<input type="submit" value="Submit" />


// the upload function
function upload(){

if(is_uploaded_file($_FILES['userfile']['tmp_name'])) {

// prepare the image for insertion
$imgData =addslashes (file_get_contents($_FILES['userfile']['tmp_name']));
// $imgData = addslashes($_FILES['userfile']);

// get the image info..
$size = getimagesize($_FILES['userfile']['tmp_name']);

// put the image in the db...
// database connection
mysql_connect($mysql_server, $mysql_user, $mysql_password) OR DIE (mysql_error());

// select the db
mysql_select_db ("database") OR DIE ("Unable to select db".mysql_error());

// our sql query
$sql = "INSERT INTO test
( image_id , image_type ,image, image_size, image_name)
('', '{$size['mime']}', '{$imgData}', '{$size[3]}', '{$_FILES['userfile']['name']}')";

// insert the image
if(!mysql_query($sql)) {
echo 'Unable to upload file';


7:30 pm on Feb 19, 2008 (gmt 0)

WebmasterWorld Senior Member demaestro is a WebmasterWorld Top Contributor of All Time 10+ Year Member

Are you getting any errors or is it just not inserting anything and not erroring?


7:39 pm on Feb 19, 2008 (gmt 0)

5+ Year Member


i hope this help

$imgData =addslashes (file_get_contents($_FILES['userfile']['tmp_name']));


$tempfile = fopen($_FILES['userfile']['tmp_name'],'rb');
$filedata = fread($tempfile,filesize($uploadfile));
$imgData = addslashes($filedata);

if no work please post the error


Featured Threads

Hot Threads This Week

Hot Threads This Month