how to upload file in php and store in database
Upload file in mysql database using php script, in this tutorial i am going to show y'all how to upload file/paradigm in mysql database using php
In this how to insert image in mysql database using php source lawmaking example; i will create a image table in database and create paradigm/file upload form. Then create file/epitome insert/save/store into database and directory using PHP MySQL.
And besides as, you tin can utilise gratis this file upload in php and mysql database script with your apps.
How to Upload Image/File in MySQL Database using PHP
- Stride 1 – Kickoff Apache Web Server
- Step 2 – Create PHP Project
- Step 3 – Create Table and Database Connection File
- Step four – Create Image File Upload Html Grade
- Pace v – Create PHP File to Insert Epitome File into DB
- Footstep 6 – Test Insert Data PHP App
Step 1 – Start Apache Spider web Server
Now, you need to offset your apache web server. And as well as start Apache and mysql web server. Y'all can see in the prototype below how to start apache and mysql server:
Step ii – Create PHP Projection
In stride 2, navigate to your xampp/htdocs/ directory. And inside xampp/htdocs/ directory, create one folder. And you lot can name this binder anything.
Here, I will "demo" the name of this binder. Then open this folder in whatsoever text editor (i will use sublime text editor).
Step three – Create Table and Database Connection File
In step 3,, you demand to create database and tabular array. And then run the following sql query to create database and table:
CREATE DATABASE my_db; CREATE Tabular array `images` ( `custId` INT(xi) NOT Cypher AUTO_INCREMENT, `file` VARCHAR(255) Zip DEFAULT Zip, `type` VARCHAR(255) Nothing DEFAULT NULL, `size` VARCHAR(255) Nothing DEFAULT NULL, PRIMARY Key (`id`) ) COLLATE='latin1_swedish_ci' ENGINE=InnoDB AUTO_INCREMENT=ane;
Then create a php file that named mydb.php. Which is used to connect phpmyadmin mysql database to projection folder(demo).
So, now create mydb.php file and add the below given code into your file:
<?php $hName='localhost'; // host name $uName='root'; // database user name $countersign=''; // database password $dbName = "my_db"; // database proper noun $dbCon = mysqli_connect($hName,$uName,$password,"$dbName"); if(!$dbCon){ die('Could non Connect MySql Server:' .mysql_error()); } ?> Step 4 – Create Image File Upload Html Form
In step 4, create a php file that named index.php. Which is used to display form image upload form. Using this form, y'all tin insert paradigm file database in php:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>how to upload file in mysql database using php - Laratutorials.com</title> </head> <body> <grade activeness="insert.php" method="post" enctype="multipart/form-information"> <h2>PHP Upload File</h2> <characterization for="file_name">Filename:</label> <input type="file" proper name="anyfile" id="anyfile"> <input type="submit" name="submit" value="Upload"> <p><strong>Annotation:</strong> Merely .jpg, .jpeg, .gif, .png formats immune to a max size of 5 MB.</p> </form> </body> </html>
Pace five – Create PHP File to Insert Image File into DB
In step 5, create i file that named insert.php file. This php file code will insert/store image file data into mysql database and directory.
Notation that, If the prototype file upload is successfully inserted / stored into database, it volition display success message. Otherwise if there is any error, it will brandish error message.
Then, add the below code into your insert.php file:
<?php include_once 'mydb.php'; // Bank check if the grade was submitted if($_SERVER["REQUEST_METHOD"] == "Postal service"){ // Bank check if file was uploaded without errors if(isset($_FILES["anyfile"]) && $_FILES["anyfile"]["error"] == 0){ $allowed = array("jpg" => "paradigm/jpg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png"); $filename = $_FILES["anyfile"]["name"]; $filetype = $_FILES["anyfile"]["type"]; $filesize = $_FILES["anyfile"]["size"]; // Validate file extension $ext = pathinfo($filename, PATHINFO_EXTENSION); if(!array_key_exists($ext, $allowed)) die("Fault: Please select a valid file format."); // Validate file size - 10MB maximum $maxsize = 10 * 1024 * 1024; if($filesize > $maxsize) die("Error: File size is larger than the allowed limit."); // Validate type of the file if(in_array($filetype, $allowed)){ // Check whether file exists before uploading it if(file_exists("upload/" . $filename)){ echo $filename . " is already exists."; } else{ if(move_uploaded_file($_FILES["anyfile"]["tmp_name"], "upload/" . $filename)){ $sql="INSERT INTO images(file,type,size) VALUES('$filename','$filetype','$filesize')"; mysqli_query($conn,$sql); echo "Your file was uploaded successfully."; }else{ echo "File is not uploaded"; } } } else{ echo "Error: At that place was a problem uploading your file. Please try again."; } } else{ echo "Error: " . $_FILES["anyfile"]["error"]; } } ?> Step half dozen – Exam Insert Information PHP App
Finally, you need to open your browser and type http://localhost/demo/ this run project on local machine.
Recommended PHP Tutorials
Source: https://laratutorials.com/file-image-upload-in-php-mysql-database-tutorial/
0 Response to "how to upload file in php and store in database"
Post a Comment