I am trying to upload some images on to my database and i want it to retrieve in my website, is it posoble? Thanks for reading my question.
Why don’t you upload the images to a folder, and insert the path names to the images in the database?
perfectly possible and there are even two completely different ways to do it.
- read the image file into the script and then save it into the database
- use a database command to load the image directly into the database
okay ill try that @felgall
Not sure if this helps, but this is how I am doing it
Code to upload
<tr height="30">
<td>Upload Main Image :</td>
<td></td>
<td><input type="hidden" name="MAX_FILE_SIZE" value="2500000"> <input type="file" name="image">
</td>
</tr>
<tr height="30">
<td>Upload Image 2 :</td>
<td></td>
<td><input type="hidden" name="MAX_FILE_SIZE" value="2500000"> <input type="file" name="image2">
</td>
</tr>
Part of PHP code
$FName = "NULL";
$FName2 = "NULL";
if ($_FILES['image']['name'])
{
$FName = md5($_FILES['image']['name'] . time()) . "." . end(explode('.', $_FILES['image']['name']));
$NewFile = "../UImages/" . $FName;
if (!move_uploaded_file($_FILES['image']['tmp_name'], $NewFile))
{
die("Failed to move file " .$_FILES['image']['tmp_name']." to " . $FName);
}
}
if ($_FILES['image2']['name'])
{
$FName2 = md5($_FILES['image2']['name'] . time()) . "." . end(explode('.', $_FILES['image2']['name']));
$NewFile = "../UImages/" . $FName2;
if (!move_uploaded_file($_FILES['image2']['tmp_name'], $NewFile))
{
die("Failed to move file " .$_FILES['image2']['tmp_name']." to " . $FName2);
}
}
$query="INSERT INTO new_equip(`id`,`image`, `image2`)
VALUES('$id','$FName','$FName2')";
Then code to display
<?php
$imagename = $row['image'];
$image2 = $row['image2'];
$image3 = $row['image3'];
$image4 = $row['image4'];
$image5 = $row['image5'];
$AddImages = array();
if ($image2 != "NULL")
{
$AddImages[] = $image2;
}
if (!file_exists("UImages/" . $imagename) || strlen($imagename) < 5)
{
$imagename = "NoImage.jpg";
}
?>
<?php
foreach ($AddImages as $Key => $Image)
{
if (!file_exists("UImages/" . $Image) || strlen($Image) < 5)
{
$Image = "NoImage.jpg";
}
echo "<a href=\"UImages/$Image\" data-lightbox=\"item\"><img src=\"UImages/$Image\" width=\"150\" height=\"150\" alt=\"Thumb\" style=\"margin-right:15px; width:150px; height:150px;border-radius: 4px;\" > </a>";
}
?>
And this is what I have in the phpmyadmin
Hope this helps a little but again, I am a total newbie when it comes to php/mysql and I am sure there are better ways, but this is how I am uploading images.
That takes care of the upload part - then you just need to load the image into the database (which the code you posted doesn’t show).
It is usually recommended that the image also be kept as a file as that is much faster to access. The database copy would serve as a backup so that the file can be recreated if anything happens to it - deleted or corrupted or whatever.
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.