Click to See Complete Forum and Search --> : storing images in mySQL database...


smickus
01-09-2008, 04:31 PM
Okay, so I wrote a function which is handled on an "upload" form. everything processes fine.


function doUpload() {
rsort($_FILES);
array_pop($_FILES);
$y = count($_FILES);
$x = 0;
$imageallow = array('image/jpeg', 'image/gif', 'image/pjpeg');
$filenames = array();
while($x<$y) {
$filename = $_FILES[$x]['name'];
$filetype = $_FILES[$x]['type'];
$filesize = $_FILES[$x]['size'];
$filetmp = $_FILES[$x]['tmp_name'];
if(!in_array($filetype, $imageallow)) $this->error("File named \"".$_FILES[$x]['name']."\" does not have an allowed file type.");
if($filesize>1024000) $this->error("File name \"".$_FILES[$x]['name']."\" has a larger file size than that allowed.");
if($filetype=="image/gif") $ext = ".gif"; else $ext = ".jpg";
$length = 0;
$random = date("Ymd-His-").$x;
$filename = $random.$ext;
array_push($filenames, $filename);
if (is_uploaded_file($filetmp)) {
$imageBLOB = addslashes(file_get_contents($_FILES[$x]['tmp_name']));
}
$imageDim = getimagesize($_FILES[$x]['tmp_name']);
$user = $this->user;
$ip = $this->ip();;
$date = date("d/m/Y");
$q = "INSERT INTO image (user, ip, date, image, imageDim, imageName, imageSize, imageType) VALUES ('$user', '$ip', '$date', '{$imageBLOB}', '$imageDim[3]', '$filename', '$filesize', '$filetype')";
$result = $this->runSQL($q);
$x++;
}
echo "success!";
}


And everything works fine except when the image is put into the blob in the database, only 64kb gets put in... Is this to do with some default limit set? How do I re-set this limit...?

And so the final article looks like this (http://www.smickus.co.uk/image/1743)
which is bad :(

scragar
01-09-2008, 04:41 PM
I would suspect the imagedim thing is causing your problems, take a look at the output of:

$foo = array(0, 1, 2, 3);
echo "stuff $foo[2] stuff";

try adding the {} around that like you already have the imageBLOB

$foo = array(0, 1, 2, 3);
echo "stuff {$foo[2]} stuff";


you may also find that this (http://dev.mysql.com/doc/refman/5.0/en/blob.html) points out possible reasons for a shortage in the file size, although I find that unlikly.

smickus
01-09-2008, 04:49 PM
no, it's still 64kb when it is uploaded..

scragar
01-09-2008, 04:58 PM
mysql_query("ALTER TABLE image MODIFY imageBLOB MEDIUMBLOB");
will change the field to a medium blob, which should be more than big enough for most images(if not use a LONGBLOB in the same query to give yourself even more space).

TJ111
01-09-2008, 05:03 PM
lol longblob. Hope you got a powerhouse of a server if your gonna be uploading 1gb+ files.

Make sure you check the settings in your php.ini file for `post_max_size` and `file_max_upload` (or maybe `max_upload_filesize`, can't remember the exact name), they can limit your file size.

smickus
01-10-2008, 12:33 AM
post_max_size is set to 8MB.
upload_max_filesize is set to 6MB.

Changed to MediumBlob...

Success.

Thank you xx

MrCoder
01-10-2008, 07:59 AM
lol longblob. Hope you got a powerhouse of a server if your gonna be uploading 1gb+ files.

Make sure you check the settings in your php.ini file for `post_max_size` and `file_max_upload` (or maybe `max_upload_filesize`, can't remember the exact name), they can limit your file size.

A MEDIUMBLOB will only hold 16meg and for the sake of an extra byte (3 bytes MEDIUMBLOB, 4 bytes LONGBLOB) for the size (pointer?) information you can hold up to 4gig.

smickus
01-11-2008, 12:21 AM
Good advice! Though there's no point in accepting larger than my post_max_size really!

MrCoder
01-11-2008, 04:04 AM
Good advice! Though there's no point in accepting larger than my post_max_size really!

Of course not, but I was not sure if you were sticking with the 8meg post_max_size.

smickus
01-11-2008, 07:14 AM
Okay, I am, thank you for the information though.