Read more image php

Store and Retrieve Image from MySQL Database using PHP

Generally, when we upload image file in PHP, the uploaded image is stored in a directory of the server and the respective image name is stored in the database. At the time of display, the file is retrieved from the server and the image is rendered on the web page. But, if you don’t want to consume the space of the server, the file can be stored in the database only. You can upload an image without storing the file physically on the server using the MySQL database. It’s very easy to store and retrieve images from the database using PHP and MySQL.

If you’re concerned about the server space and need free space on your server, you can insert the image file directly in the database without uploading it to the directory of the server. This procedure helps to optimize the server space because the image file content is stored in the database rather than the server. In this tutorial, we will show you how to store image files into the MySQL database and retrieve images from the database using PHP.

Before getting started to integrate file upload with the database, take a look at the file structure.

store_retrieve_image_from_database/ ├── dbConfig.php ├── index.php ├── upload.php ├── view.php └── css/ └── style.css

Insert Image File in MySQL

MySQL has a BLOB (binary large object) data type that can hold a large amount of binary data. The BLOB data type is perfect for storing image data in the database. In MySQL, four BLOB types are available – TINYBLOB, BLOB, MEDIUMBLOB, and LONGBLOB. The LONGBLOB data type is perfect to store the image file data.

Читайте также:  Wget html to file

Create Database Table

To store the file content, a table is required in the database. The following SQL creates an images table with the LONGBLOB data type field in the MySQL database.

CREATE TABLE `images` ( `id` int(11) NOT NULL AUTO_INCREMENT, `image` longblob NOT NULL, `created` datetime NOT NULL DEFAULT current_timestamp(), PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Database Configuration (dbConfig.php)

The dbConfig.php file is used to connect and select the database. Specify the database host ( $dbHost ), username ( $dbUsername ), password ( $dbPassword ), and name ( $dbName ) as per your MySQL database credentials.

// Database configuration 
$dbHost = "localhost";
$dbUsername = "root";
$dbPassword = "root";
$dbName = "codexworld";

// Create database connection
$db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);

// Check connection
if ($db->connect_error) <
die(
"Connection failed: " . $db->connect_error);
>

Image Upload Form

Create an HTML form with a file input field to select an image file for upload. Make sure the < form >tag contains the following attributes.

form action="upload.php" method="post" enctype="multipart/form-data"> label>Select Image File: label> input type="file" name="image"> input type="submit" name="submit" value="Upload"> form>

Store Image File in Database (upload.php)

The upload.php file handles the image upload and database insertion process.

  • Check whether the user selects an image file to upload.
  • Retrieve the content of image file by the tmp_name using PHP file_get_contents() function.
  • Insert the binary content of the image in the database using PHP and MySQL.
  • Show the image uploading status to the user.
// Include the database configuration file 
require_once 'dbConfig.php';

// If file upload form is submitted
$status = $statusMsg = '';
if(isset(
$_POST["submit"])) <
$status = 'error';
if(!empty(
$_FILES["image"]["name"])) <
// Get file info
$fileName = basename($_FILES["image"]["name"]);
$fileType = pathinfo($fileName, PATHINFO_EXTENSION);

// Allow certain file formats
$allowTypes = array('jpg','png','jpeg','gif');
if(
in_array($fileType, $allowTypes)) <
$image = $_FILES['image']['tmp_name'];
$imgContent = addslashes(file_get_contents($image));

// Insert image content into database
$insert = $db->query("INSERT into images (image, created) VALUES ('$imgContent', NOW())");

if(
$insert) <
$status = 'success';
$statusMsg = "File uploaded successfully.";
>else <
$statusMsg = "File upload failed, please try again.";
>
>else <
$statusMsg = 'Sorry, only JPG, JPEG, PNG, & GIF files are allowed to upload.';
>
>else <
$statusMsg = 'Please select an image file to upload.';
>
>

// Display status message
echo $statusMsg;
?>

Retrieve image from database (view.php)

In the view.php file, we will retrieve the image content from the MySQL database and list them on the web page.

  • The data, charset, and base64 parameters in the src attribute, are used to display image BLOB from MySQL database.
// Include the database configuration file 
require_once 'dbConfig.php';

// Get image data from database
$result = $db->query("SELECT image FROM images ORDER BY id DESC");
?> if($result->num_rows > 0) ?>
div class="gallery">

while($row = $result->fetch_assoc()) ?>
img src="data:image/jpg;charset=utf8;base64, echo base64_encode($row['image']); ?>" />
> ?>
div
>

>else ?>
p class="status error">
Image(s) not found. p
>

> ?>

Conclusion

This tutorial helps you to integrate file upload functionality without storing files on the server. You can use this example script to upload & store images in the database, and fetch images from the database, and display them on the webpage using PHP and MySQL. To make the image upload process user-friendly, use jQuery to upload files with progress bar using Ajax and PHP.

Are you want to get implementation help, or modify or enhance the functionality of this script? Click Here to Submit Service Request

If you have any questions about this script, submit it to our QA community — Ask Question

Источник

I’m a coder. Welcome to my blog. Here are some of the records on my job.

Home

Categories

Read more php images

I have two separate files, one is to display the html/php document image, and the other is a php file that renders the image using the header function content-type:image/jpeg.

I tried using it with one image and it works well. However, I need to display multiple images. How could I do this?

The html/php doc has an img tag that points out to the php file that renders the image

$selectimage = mysql_query("SELECT Image from ImageTbl", $con); if($selectimage) < header("Content-type:image/jpeg"); while($row = mysql_fetch_array($selectimage)) < echo $row["Image"]; >> 

make two files one for image another for fetching the row like this

$image_id = $_GET["id"]; header("Content-type:image/jpeg"); //query database to get only one image from id echo $row["Image"]; 
//query for image data while($row = mysql_fetch_array()) < echo ""; > 

Joomla 3.1 intro image as read more link

In joomla 3.1 I edited in this file \components\com_content\views\featured\tmpl\default_item.php image_intro) && !empty($images->image_intro)) : ?> float_intro)) ? $param

Limit the length of the text in php and provide a «Read more» link

I have text stored in the php variable $text. This text can be 100 or 1000 or 10000 words. As currently implemented, my page extends based on the text, but if the text is too long the page looks ugly. I want to get the length of the text and limit th

I have a jSfiddle file here. I want to add a read more link after the image or after certain character limit maybe around 100. But i’m having issues grabbing the link of the page title. I want to make the script dynamic. Right now my jQuery is writte

PHP — Read more returns an ‘indefinite variable’ error

I’ve been working on a ‘read more’ page for quite a while now. What I basically want is when you click read more, it will redirect to the news item page. Index.php $p=mysqli_query($con, «SELECT id, titel, tekst, datum FROM nieuws LIMIT $start, $limit

How to get php image data on javascript copy-n-paste with XMLHttpRequest

I try to make an image-upload functionality similar to the one GMail uses. You copy (CTRL-C) an image from your desktop and paste (CTRL-V) it onto the website. The image is then uploaded via a XMLHttpRequest to a php-script that handles the incoming

how can i hide & ldquo; read more & rdquo; on teaser

I created a view displaying some teasers that link to my galleries on http://quaaoutlodge.com/gallery. You note the «read more» links on top of the images, I’d like to get rid of them but am unsure how. Any help or assistance would be appreciate

Text in limited characters div, add & ldquo; Read more & rdquo; Connect and display all characters when the link is clicked

I’ve a div with text inside that is displayed using PHP & MySQL, the structure is like this:

Here is a lot of text.

I want to display a «Read more» link when the tex

Expand the div to & ldquo; Read more & rdquo;

I come across a problem in my site to which I am finding a solution. Posts are fetched from the MySQL database. If it contains a few lines,it is displayed completely.But,if it contains too many lines (thus taking more space on the browser screen),the

Unable to change & ldquo; Read more & rdquo; option on this theme WordPress

I have this theme and when I want to add the «Read more» option using the «Insert More Tag», the theme adds a read more icon below. What I want to do: remove the «read more» icon and show the «read more» at the end

Create a pagination as well as a selection of number per page on the PHP image gallery

I’ve got a simple PHP image gallery where the PHP reads data from a MySQL database and organizes and links each one to a larger version. It works well but places too many images on the page. I’m hoping someone can show me how to first split the image

How do I create a Read More button in a table?

What’s more I have to implement a & ldquo; read more & rdquo; functionality

I want to implement a «read more» feature when I’m displaying content. I want to cut off the text either at 6 lines or by number of words (but prefer by lines, unless you have different advice). I’m guessing I will have to use javascript for thi

I’m trying to make a Read More button, but I just started using javascript / jquery

I want to do a Read More button, which will expand the page and the text will appear with a nice animation. I tried doing a jquery code but I realised that it was not going to work and I deleted it. The Read More button will appear if the text is big

Источник

Оцените статью