How to Upload a File to Sql Database Html

Server-side file upload tin can be hands implemented using PHP. In that location are various ways available to upload epitome to server and brandish images on the webpage. Mostly, in a dynamic web application, the uploaded image is stored in a directory of the server and the file proper noun is inserted in the database. Afterwards, the images are retrieved from the server based on the file proper name stored in the database and brandish on the web folio.

The image tin be uploaded directly to the database without storing on the server. But it volition increase the database size and web page load time. So, it's always a good idea to upload images to the server and store file names in the database. In this tutorial, nosotros volition show you lot the unabridged process to upload and store the paradigm file in MySQL database using PHP.

The example code demonstrates the process to implement the file upload functionality in the web application and the following functionality will be implemented.

  • HTML course to upload epitome.
  • Upload image to server using PHP.
  • Store file name in the database using PHP and MySQL.
  • Remember images from the database and brandish in the spider web page.

Create Datbase Table

To store the image file name a table demand to be created in the database. The following SQL creates an images table with some basic fields in the MySQL database.

          CREATE          TABLE          `images` (          `id`          int(11)          Not Zilch          AUTO_INCREMENT,          `file_name`          varchar(255) COLLATE utf8_unicode_ci          Non Zilch,          `uploaded_on`          datetime          NOT NULL,          `condition`          enum('1','0') COLLATE utf8_unicode_ci          Non Null          DEFAULT          'ane',          Main 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 MySQL database. Specify the database hostname ($dbHost), username ($dbUsername), countersign ($dbPassword), and name ($dbName) equally per your MySQL credentials.

          <?php
// Database configuration
$dbHost = "localhost" ;
$dbUsername = "root" ;
$dbPassword = "root" ;
$dbName = "codexworld" ; // Create database connexion
$db = new mysqli ( $dbHost , $dbUsername , $dbPassword , $dbName ); // Bank check connection
if ( $db -> connect_error ) {
    dice(
"Connectedness failed: " . $db -> connect_error );
}
?>

Upload Form HTML

Create an HTML form to permit the user to choose a file they want to upload. Brand certain <form> tag contains the following attributes.

  • method="postal service"
  • enctype="multipart/form-information"

As well, make sure <input> tag contains blazon="file" attribute.

<form          activeness="upload.php"          method="post"          enctype="multipart/form-information">     Select Paradigm File to Upload:     <input          blazon="file"          name="file">     <input          type="submit"          proper name="submit"          value="Upload"> </form>        

php-upload-image-file-to-server-codexworld

The file upload class volition be submitted to the upload.php file to upload prototype to the server.

Upload File to Server and Store in Database (upload.php)

The upload.php file handles the paradigm upload functionality and shows the condition message to the user.

  • Include the database configuration file to connect and select the MySQL database.
  • Become the file extension using pathinfo() office in PHP and validate the file format to check whether the user selects an image file.
  • Upload image to server using move_uploaded_file() function in PHP.
  • Insert epitome file name in the MySQL database using PHP.
  • Upload status will exist shown to the user.
          <?php
// Include the database configuration file
include 'dbConfig.php' ;
$statusMsg = '' ; // File upload path
$targetDir = "uploads/" ;
$fileName = basename ( $_FILES [ "file" ][ "proper name" ]);
$targetFilePath = $targetDir . $fileName ;
$fileType = pathinfo ( $targetFilePath , PATHINFO_EXTENSION );

if(isset(

$_POST [ "submit" ]) && !empty( $_FILES [ "file" ][ "name" ])){
// Allow certain file formats
$allowTypes = assortment( 'jpg' , 'png' , 'jpeg' , 'gif' , 'pdf' );
    if(
in_array ( $fileType , $allowTypes )){
// Upload file to server
if( move_uploaded_file ( $_FILES [ "file" ][ "tmp_name" ], $targetFilePath )){
// Insert image file name into database
$insert = $db -> query ( "INSERT into images (file_name, uploaded_on) VALUES ('" . $fileName . "', NOW())" );
            if(
$insert ){
$statusMsg = "The file " . $fileName . " has been uploaded successfully." ;
            }else{
$statusMsg = "File upload failed, please try once more." ;
            }
        }else{
$statusMsg = "Pitiful, at that place was an error uploading your file." ;
        }
    }else{
$statusMsg = 'Sorry, only JPG, JPEG, PNG, GIF, & PDF files are allowed to upload.' ;
    }
}else{
$statusMsg = 'Delight select a file to upload.' ;
}
// Display status message
echo $statusMsg ;
?>

Display Images from Database

Now we volition retrieve the uploaded images from the server based on the file names in the database and display images in the web page.

  • Include the database configuration file.
  • Fetch images from MySQL database using PHP.
  • Listing images from the uploads directory of the server.
          <?php
// Include the database configuration file
include 'dbConfig.php' ; // Go images from the database
$query = $db -> query ( "SELECT * FROM images ORDER Past uploaded_on DESC" );

if(

$query -> num_rows > 0 ){
    while(
$row = $query -> fetch_assoc ()){
$imageURL = 'uploads/' . $row [ "file_name" ];
?> <img src="<?php echo $imageURL ; ?>" alt="" /> <?php }
}else{
?> <p>No image(s) found...</p> <?php } ?>

upload-image-retrieve-from-database-php-mysql-codexworld

Create Dynamic Image Gallery with jQuery, PHP & MySQL

Decision

Hither we accept shown the almost effective way to implement image upload functionality on the website. You lot can easily extend the file upload functionality as per your requirements. For providing the improve user-interface you tin can integrate the Ajax File Upload functionality.

Upload multiple images using jQuery, Ajax and PHP

Are you want to get implementation help, or modify or heighten the functionality of this script? Submit Paid Service Asking

If you have any questions well-nigh this script, submit it to our QA customs - Ask Question

blockantiont.blogspot.com

Source: https://www.codexworld.com/upload-store-image-file-in-database-using-php-mysql/

Related Posts

0 Response to "How to Upload a File to Sql Database Html"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel