uploading images to mysql using php

PHP:
<?php if(!isset($_FILES['userfile'])) {
	echo '

Please select a file</p>';
}
else{
	try {
		upload();
		// give praise and thanks to the php gods
		echo '

Thank you for submitting</p>';
		}
	catch(Exception $e) {
		echo $e->getMessage();
		echo 'Sorry, could not upload file';
	}

}

function upload(){
	 
	if(is_uploaded_file($_FILES['userfile']['tmp_name'])) {
		// check the file is less than the maximum file size
		if($_FILES['userfile']['size'] < $maxsize)
		{
			// 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("localhost", "root") OR DIE (mysql_error());
			// select the db
			mysql_select_db ("project") OR DIE ("Unable to select db".mysql_error());
			// SQL query for insertion
			$sql = "INSERT INTO images ( imageid , imagetype ,image, imagesize, imagename) VALUES ('', '{$size['mime']}', '{$imgData}', '{$size}', '{$_FILES['userfile']['name']}')";
			//insert the image
			if(!mysql_query($sql)) {
				echo 'Unable to upload file';
			}
		}
	}
	else {
		//if thefile is not less than the maximum allowed, print an error
		echo '<div>File exceeds the Maximum File limit</div> <div>Maximum File limit is '.$maxsize.'</div> <div>File '.$_FILES['userfile']['name'].' is '.$_FILES['userfile']['size'].' bytes</div> <hr />';
	}
} ?>

and the html file is

Code:
<html>
	<head><title>File Upload To Database</title></head>
	<body>
		<h3>Please Choose a File and click Submit</h3>
		<form enctype="multipart/form-data" action="php/upload.php" method="post">
			<input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
			<input name="userfile[]" type="file" />
			<input type="submit" value="Submit" />
		</form>
	</body>
</html>

The output I get is

File exceeds the Maximum File limit
Maximum File limit is
File Array is Array bytes

Thank you for submitting

I am a noob with this, what am I doing wrong?
 
i dont think its the limit, most likely a mismatch between the input field names in HTML and PHP and also some errors in the PHP itself, what are those dots in the last line of code where the error is logged? and the names in the $FILES map look suspicious
 
database design here goes,



I am trying to upload an image less than 10kb, so the php.ini problem should not happen here!

@vishalrao: the dots are to concatenate the string in php
 
Back
Top