How Do I Upload A File To My Website
Server-side file upload can exist 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 proper noun is inserted in the database. Later, the images are retrieved from the server based on the file name stored in the database and display on the web folio.
The epitome can exist uploaded directly to the database without storing on the server. Just information technology will increase the database size and web page load time. Then, it'due south always a adept idea to upload images to the server and store file names in the database. In this tutorial, we will show you the entire process to upload and store the image file in MySQL database using PHP.
The case lawmaking demonstrates the process to implement the file upload functionality in the web application and the following functionality will be implemented.
- HTML form to upload image.
- Upload image to server using PHP.
- Shop file proper noun in the database using PHP and MySQL.
- Recollect images from the database and display in the web folio.
Create Datbase Table
To shop the image file name a table need to exist created in the database. The following SQL creates an images
tabular array with some basic fields in the MySQL database.
CREATE Table `images` ( `id` int(11) Non NULL AUTO_INCREMENT, `file_name` varchar(255) COLLATE utf8_unicode_ci NOT Zip, `uploaded_on` datetime Non Cypher, `status` enum('1','0') COLLATE utf8_unicode_ci NOT Zilch DEFAULT 'ane', PRIMARY Cardinal (`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
), password ($dbPassword
), and name ($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 ); // Check connection
if ( $db -> connect_error ) {
die( "Connection failed: " . $db -> connect_error );
}
?>
Upload Form HTML
Create an HTML form to let the user to choose a file they want to upload. Make certain <form> tag contains the post-obit attributes.
- method="post"
- enctype="multipart/form-data"
Also, make sure <input> tag contains type="file"
aspect.
<form activeness="upload.php" method="mail service" enctype="multipart/course-data"> Select Image File to Upload: <input type="file" name="file"> <input type="submit" name="submit" value="Upload"> </form>
The file upload course will be submitted to the upload.php
file to upload image to the server.
Upload File to Server and Store in Database (upload.php)
The upload.php
file handles the image 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() function 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 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" ][ "name" ])){
// Permit sure 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 . "', At present())" );
if( $insert ){
$statusMsg = "The file " . $fileName . " has been uploaded successfully." ;
}else{
$statusMsg = "File upload failed, please endeavour over again." ;
}
}else{
$statusMsg = "Sorry, there was an error uploading your file." ;
}
}else{
$statusMsg = 'Lamentable, only JPG, JPEG, PNG, GIF, & PDF files are allowed to upload.' ;
}
}else{
$statusMsg = 'Please select a file to upload.' ;
} // Brandish status message
echo $statusMsg ;
?>
Display Images from Database
At present nosotros will recall the uploaded images from the server based on the file names in the database and display images in the spider web folio.
- 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' ; // Become 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 repeat $imageURL ; ?>" alt="" /> <?php }
}else{ ?> <p>No image(s) institute...</p> <?php } ?>
Create Dynamic Image Gallery with jQuery, PHP & MySQL
Conclusion
Here we have shown the nearly effective way to implement paradigm upload functionality on the website. You can easily extend the file upload functionality as per your requirements. For providing the ameliorate user-interface y'all can integrate the Ajax File Upload functionality.
Upload multiple images using jQuery, Ajax and PHP
Are yous want to get implementation help, or modify or enhance the functionality of this script? Submit Paid Service Request
If you accept any questions almost this script, submit it to our QA customs - Ask Question
Source: https://www.codexworld.com/upload-store-image-file-in-database-using-php-mysql/
Posted by: tuckersibareaught.blogspot.com
0 Response to "How Do I Upload A File To My Website"
Post a Comment