I followed this tutorial to make an image uploader. It works fine, except when I try to upload larger file sizes: It just 'changes' the file size to 64KB and only displays a small part of a larger image. So, I googled and found that 'blob' has a maximum file size of 64kb, so I changed it to longblob, which has a max file size of 4GB.
But now, trying to upload large images (1MB and larger), I get the SQL error 'MySQL server has gone away'. With images smaller than 1MB, the upload works. An image of 1400x900 with a file size of 915kb uploads, but an image of 1400x900 with a file size of 1.6mb doesn't. A gif of 250x179 with a file size of 1mb does upload. It seems like if the file size is larger than 1MB that the SQL insert function in the PHP simply doesn't connect anymore: How is this possible?
My php.ini has a maximum file size of 32MB, so that's not the issue. I am using MAMP.
HTML:
<form action="tutorial.php" method="POST" enctype="multipart/form-data">
File:
<input type="file" name="image" > <input type="submit" value="Upload">
</form>
PHP:
// connect to database
include 'include/config.php';
// file properties
$file = $_FILES['image']['tmp_name'];
if (!isset($file)){
echo "Please select an image.";
} else {
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name = addslashes($_FILES['image']['name']);
$image_size = getimagesize($_FILES['image']['tmp_name']);
if ($image_size==FALSE){
echo "This is not an image";
}else{
if(!$insert = mysql_query("INSERT INTO gallery VALUES ('', '$image_name', '$image')")){
die(mysql_error());
echo "Problem uploading the image.";
}else{
$lastid = mysql_insert_id();
echo "Image uploaded.<p />Your image:<p /><image src=include/get.php?id=$lastid>";
}
}
}
PHP config:
//database credentials
$username = "root";
$password = "root";
$hostname = "localhost";
//connection to the database
mysql_connect($hostname, $username, $password) or die(mysql_error());
mysql_select_db('imandra') or die(mysql_error());
//echo "Connected to MySQL";
PHP get:
include 'config.php';
$id= addslashes($_REQUEST['id']);
$image = mysql_query("SELECT * FROM gallery WHERE id='$id'");
$image = mysql_fetch_assoc($image);
$image = $image['image'];
header("Content-type: image/jpeg");
echo $image;
You might want to take a look at the settings in your mysql configuration file (my.cnf).
Increase the size of the max_allowed_packet setting, e.g.
max_allowed_packet=50M
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With