I am having issues storing a .JPG
image in my MySQL database. I am using the PHP code below to store it into the database (in long blob form). My code appears to be completely error free and every thing seems to act as it should aside from the fact that the image is only stored in what appears to be 36 B size on PHPMyAdmin.
I would expect the image to be much larger (22 MB supposedly).
<?php
require '../FirstWebsite/CommonFunctions.php';
DB_Connect();
$dblink = DB_Connect();
?>
<html>
<body>
<form method="post" enctype="multipart/form-data">
<br/>
<input type="file" name="selectImage">
<br/>
<input type="submit" name="submitImage" value="submit">
</form>
<?php
if(isset($_POST['submitImage'])){
if(getimagesize($_FILES['selectImage']['tmp_name'])==FALSE){
echo 'please select an actual image';
}
else{
$getImage = mysqli_real_escape_string($dblink, $_FILES['selectImage']['tmp_name']);
$name = mysqli_real_escape_string($dblink, $_FILES['selectImage']['name']);
$image = base64_encode($getImage);
saveImage($name, $image, $dblink);
}
}
function saveImage($name, $image, $dblink){
$query = "INSERT INTO trialtable2 (caption, actualpicture) VALUES ('$name','$image')";
$queryCheck = mysqli_query($dblink, $query);
if($queryCheck == TRUE){
echo '<br/> uploaded';
}
else{
echo '<br/> not uploaded';
}
}
?>
</body>
</html>
what I have done
attempted uploading .jpeg
.JPG
.jpg
(same but still...) .tif
.png
file_upload = on
(or whatever) is already on in php.ini
it is a long blob type in the data base table
Supposedly storing images in a database is no the way to go however this is what I am working with for the time being.
versions
I believe that this question is not a replica as I haven't been able to find an answer elsewhere. If I am doing something incorrectly feel free to let me know as I am still new to the community.
tmp_name
is the temporary path to the file, this is why you're only seeing a few bytes.
addslashes
).file_get_contents($_FILES['selectImage']['tmp_name'])
to get the actual contents of the image for inserting into your database.It generally isn't a great idea to store the blobs in the database, and better to store the path (like you're currently doing). If you choose to go the path route, you need to use something like move_uploaded_file
to move the file to a persistent location.