created database described in following page to the letter
got the following code
(corrected 2 errors - a missing } and defining $maxsize)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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 echo $_SERVER['PHP_SELF'];?>" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
<div><input name="userfile[]" type="file" /></div>
<div><input type="submit" value="Submit" /></div>
</form>
</body></html>
<?php
// check if a file was submitted
if(!isset($_FILES['userfile']))
{
echo '<p>Please select a file</p>';
}
else
{
try{
upload();
// give praise and thanks to the php gods
echo '<p>Thank you for submitting</p>';
}
catch(Exception $e){
{
echo $e->getMessage();
echo 'Sorry, could not upload file';
}
}
}
?>
<?php
// the upload function
function upload(){
$maxsize = $_POST['MAX_FILE_SIZE'];
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", "$USER", "$PASSWORD") OR DIE (mysql_error());
// select the db
mysql_select_db ("$DB") OR DIE ("Unable to select db".mysql_error());
// our sql query
$sql = "INSERT INTO testblob ( image_id , image_type ,image, image_size, image_name) VALUES ('', '{$size['mime']}', '{$imgData}', '{$size[3]}', '{$_FILES['userfile']['name']}')";
// insert the image
if(!mysql_query($sql))
{
echo 'Unable to upload file';
}
}
}
else
{
// if the file 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 />';
}
}
?>
i get the following error
[29-Jun-2005 23:48:55] PHP Notice: Array to string conversion in C:\location\to\script\upload.php on line 45
this is line 45
if(is_uploaded_file($_FILES['userfile']['tmp_name']))
and nothing is uploaded
so i cannot test this ability, and still curious