How to Insert Upload Image Into Database

How to insert image in database using PHP

In this article, you will acquire how to insert an prototype in the database using PHP. In some applications, there may be a requirement to develop for image upload. Database is preferred best to store data and the file system is the best place to store files. For storing an epitome in a database, we can store the name or path of the image in the database and store the paradigm equally a file on the server. So that the web server can easily admission it where we want or send information technology to the company.

PHP provides the easiest fashion for uploading and storing images in the MySQL database and saved the image in a particular location. First, let's create an HTML course that allow users to choose the prototype file they want to upload. The enctype='multipart/form-information' form attributes allow files to exist sent through postal service.

                      <class method='mail' action='#' enctype='multipart/course-data'>            <div course="form-grouping">            <input type="file" name="image" >            </div>            <div form="form-grouping">            <input type='submit' name='submit' value='Upload' class="btn btn-main">            </div>            </form>        

The course looks something similar this -

PHP store image in MySQL

Next, create a database to store files. Y'all can either copy paste this CREATE statement in your database or apply your existing one.

          CREATE Table `image` (  `id` int(11) Not Zilch AUTO_INCREMENT,  `file_name` varchar(255) COLLATE utf8_unicode_ci NOT Zip,  `uploaded_on` datetime Non Nada,  `condition` enum('1','0') COLLATE utf8_unicode_ci NOT NULL DEFAULT '1',  PRIMARY Cardinal (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;        

Next, we have written the database connexion code. Make sure to replace 'hostname', 'username', 'countersign' and 'database' with your database credentials and name.

          $conn = mysqli_connect('hostname', 'username', 'countersign', 'database'); //Check for connectedness error if($conn->connect_error){   die("Error in DB connection: ".$conn->connect_errno." : ".$conn->connect_error);     }        

Next, we have written code to check the submitted paradigm, valid file extension and insert it into the database. The move_uploaded_file() function of PHP moves an uploaded file to a new destination. It returns TRUE on success, FALSE on failure.

          if(isset($_POST['submit'])){  	$filename = $_FILES['image']['name']; 	 	// Select file type 	$imageFileType = strtolower(pathinfo($filename,PATHINFO_EXTENSION)); 	 	// valid file extensions 	$extensions_arr = array("jpg","jpeg","png","gif");   	// Check extension 	if( in_array($imageFileType,$extensions_arr) ){   	// Upload files and store in database 	if(move_uploaded_file($_FILES["prototype"]["tmp_name"],'upload/'.$filename)){ 		// Prototype db insert sql 		$insert = "INSERT into image(file_name,uploaded_on,condition) values('$filename',at present(),1)"; 		if(mysqli_query($conn, $insert)){ 		  repeat 'Data inserted successfully'; 		} 		else{ 		  echo 'Error: '.mysqli_error($conn); 		} 	}else{ 		echo 'Error in uploading file - '.$_FILES['image']['name'].'
'; } } }

Complete Code - Insert image in database using PHP

Here, nosotros take merged the above code to upload image to the database.

                      <?php   // database Connectedness $conn = mysqli_connect('hostname', 'username', 'password', 'database'); // check for connection error if($conn->connect_error){   die("Error in DB connectedness: ".$conn->connect_errno." : ".$conn->connect_error);     }  if(isset($_POST['submit'])){  	$filename = $_FILES['image']['name']; 	 	// Select file type 	$imageFileType = strtolower(pathinfo($filename,PATHINFO_EXTENSION)); 	 	// valid file extensions 	$extensions_arr = array("jpg","jpeg","png","gif");   	// Check extension 	if( in_array($imageFileType,$extensions_arr) ){   	// Upload files and store in database 	if(move_uploaded_file($_FILES["prototype"]["tmp_name"],'upload/'.$filename)){ 		// Paradigm db insert sql 		$insert = "INSERT into image(file_name,uploaded_on,status) values('$filename',now(),1)"; 		if(mysqli_query($conn, $insert)){ 		  echo 'Data inserted successfully'; 		} 		else{ 		  repeat 'Error: '.mysqli_error($conn); 		} 	}else{ 		echo 'Error in uploading file - '.$_FILES['image']['name'].'<br/>'; 	} 	} }  ?>            <html>            <head>            <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">            <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.iii.ane/js/bootstrap.min.js"></script>            </head>            <torso>            <div class="container">            <h1>Select Image to Upload</h1>            <grade method='postal service' action='#' enctype='multipart/form-data'>            <div class="form-group">            <input type="file" name="image" id="file" multiple>            </div>            <div class="course-grouping">            <input type='submit' name='submit' value='Upload' form="btn btn-primary">            </div>            </grade>            </div>            </body>            </html>        

PHP store image in MySQL

Related Articles

PHP contrary a string without predefined function
PHP random quote generator
PHP convert string into an array
PHP remove HTML and PHP tags from cord
Import Excel File into MySQL using PHP
PHP array length
Import Excel File into MySQL Database using PHP
PHP String Contains
How to display PDF file in PHP from database
How to read CSV file in PHP and store in MySQL
Create And Download Word Document in PHP
PHP SplFileObject Standard Library
Simple File Upload Script in PHP
Sending form data to an email using PHP
Recover forgot countersign using PHP and MySQL
Php file based authentication
Simple PHP File Cache
How to get electric current directory, filename and code line number in PHP
Insert in database without page refresh PHP

jonestherfleater.blogspot.com

Source: https://www.etutorialspoint.com/index.php/411-how-to-insert-image-in-database-using-php

0 Response to "How to Insert Upload Image Into Database"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel