Php File Upload

It is simple lớn upload image in PHPhường MySQL database và display image with example and code.

Bạn đang xem: Php file upload

Store data in the database using INSERT INTO query and retrieve sầu data from the database using SELECT query.

See the example below:


*

*

Step 1: Connection with Database

The dbConn.php tệp tin is used to connect with the database.

Make dbConn.php tệp tin as a common file, for reusability which always connected with MySquốc lộ database. Include only the dbConn.php file name on that file where you want khổng lồ perform a task with the database. You don"t need to make a connection every time in every tệp tin.


$db = mysqli_connect("localhost","root","","testDB");if(!$db) die("Connection failed: " . mysqli_connect_error());?>

Step 2: Store & Retrieve sầu image from Database

Here, we are storing an image inkhổng lồ the database và retrieve sầu an image from the database inlớn the table format.

The index.php tệp tin is using for both purposes, which are inserting images và displaying images from the database.

Xem thêm: Nằm Mơ Thấy Cãi Nhau Với Chồng Cãi Nhau Là Điềm Gì, Tốt Hay Xấu?

In this file, we are using a table for displaying records & images in the proper format.

After storing data, we are fetching those data at a time using mysqli_fetch_array() function from the database. You can see the above sầu example there are fetched data with the images.

Note: You also have sầu khổng lồ create a thư mục for storing images. In this example, we have sầu created a folder whose name is all_images.

In this example, you can see the data in the table format for proper view. And we have also mix the width và height of those images. You can also define image size for your view.

After completing all, cđại bại the database connection.


Store and retrieve image from database in PHPinclude "dbConn.php"; // Using database connection tệp tin hereif(isset($_POST<"submit">)) $var1 = rand(1111,9999); // generate random number in $var1 variable $var2 = rand(1111,9999); // generate random number in $var2 variable $var3 = $var1.$var2; // concatenate $var1 & $var2 in $var3 $var3 = md5($var3); // convert $var3 using md5 function and generate 32 characters hex number $fnm = $_FILES<"image"><"name">; // get the image name in $fnm variable $dst = "./all_images/".$var3.$fnm; // storing image path into the all_images folder with 32 characters hex number & file name $dst_db = "all_images/".$var3.$fnm; // storing image path inlớn the database with 32 characters hex number & tệp tin name move_uploaded_file($_FILES<"image"><"tmp_name">,$dst); // move sầu image inkhổng lồ the all_images thư mục with 32 characters hex number và image name $check = mysqli_query($db,"insert inkhổng lồ tbltest(fname,images) values("$_POST","$dst_db")"); // executing insert query if($check) echo ""; // alert message else echo ""; // when error occur ?>

Insert Data

All Records

border="2"> $records = mysqli_query($db,"select * from tbltest"); // fetch data from databasewhile($data = mysqli_fetch_array($records))?> ?>
Sr.No. Name Images
emang lại $data<"id">; ?> emang lại $data<"fname">; ?> ; ?>" width="100" height="100">
mysqli_close($db); // cthua thảm connection ?>