Program Local Files to Be Uploaded Into a Mysql Server

Server-side file upload can be easily implemented using PHP. There are various means available to upload image to server and display images on the webpage. Generally, in a dynamic spider web application, the uploaded image is stored in a directory of the server and the file name is inserted in the database. Later, the images are retrieved from the server based on the file proper noun stored in the database and display on the spider web folio.

The image can be uploaded directly to the database without storing on the server. Merely it will increase the database size and web page load fourth dimension. So, information technology's always a good thought to upload images to the server and store file names in the database. In this tutorial, we volition prove you the unabridged procedure to upload and store the image file in MySQL database using PHP.

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

  • HTML course to upload prototype.
  • Upload image to server using PHP.
  • Store file name in the database using PHP and MySQL.
  • Retrieve images from the database and display in the web page.

Create Datbase Table

To store the paradigm file proper noun a tabular array need to exist created in the database. The following SQL creates an images table with some basic fields in the MySQL database.

          CREATE          TABLE          `images` (          `id`          int(xi)          Non NULL          AUTO_INCREMENT,          `file_name`          varchar(255) COLLATE utf8_unicode_ci          Not NULL,          `uploaded_on`          datetime          NOT NULL,          `status`          enum('one','0') COLLATE utf8_unicode_ci          NOT NULL          DEFAULT          'i',          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 MySQL database. Specify the database hostname ($dbHost), username ($dbUsername), countersign ($dbPassword), and proper noun ($dbName) as per your MySQL credentials.

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

Upload Form HTML

Create an HTML class to allow the user to choose a file they desire to upload. Brand sure <form> tag contains the post-obit attributes.

  • method="postal service"
  • enctype="multipart/class-data"

Also, make sure <input> tag contains type="file" aspect.

<form          activity="upload.php"          method="mail service"          enctype="multipart/grade-data">     Select Image File to Upload:     <input          blazon="file"          proper noun="file">     <input          type="submit"          name="submit"          value="Upload"> </form>        

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

The file upload grade will exist submitted to the upload.php file to upload paradigm to the server.

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

The upload.php file handles the epitome upload functionality and shows the status bulletin to the user.

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

if(isset(

$_POST [ "submit" ]) && !empty( $_FILES [ "file" ][ "proper noun" ])){
// Allow certain file formats
$allowTypes = array( '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 attempt once again." ;
            }
        }else{
$statusMsg = "Deplorable, there was an mistake uploading your file." ;
        }
    }else{
$statusMsg = 'Sorry, only JPG, JPEG, PNG, GIF, & PDF files are immune to upload.' ;
    }
}else{
$statusMsg = 'Please select a file to upload.' ;
}
// Display status message
echo $statusMsg ;
?>

Display Images from Database

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

  • Include the database configuration file.
  • Fetch images from MySQL database using PHP.
  • List 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 Prototype Gallery with jQuery, PHP & MySQL

Conclusion

Here we accept shown the near effective way to implement image upload functionality on the website. Yous can easily extend the file upload functionality as per your requirements. For providing the ameliorate user-interface you tin integrate the Ajax File Upload functionality.

Upload multiple images using jQuery, Ajax and PHP

Are y'all want to get implementation help, or alter or heighten the functionality of this script? Submit Paid Service Request

If yous have any questions about this script, submit it to our QA customs - Ask Question

reedaredy1976.blogspot.com

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

0 Response to "Program Local Files to Be Uploaded Into a Mysql Server"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel