How to Upload Image in Database Using C#
Uploading the image/videos into the database and display it using PHP is the way of uploading the epitome into the database and fetched information technology from the database. Using the PHP code, the user uploads the epitome or videos they are safely getting entry into the database and the images should be saved into a particular location past fetching these images from the database.
If any of the websites contain the functionality to upload images/videos with some detail, so by using this code we will upload the image into your database and whether you would similar to ascertain what the person has got to be uploaded. And by this code the epitome which is uploaded that where save in your system where you are given the location.
First, create the database on XAMPP/WAMP server using phpMyAdmin and give the database name is photos and the tabular array name is image. The table contains two fields:
- Id – int(11)
- Filename – VARCHAR(100)
Id should be in Automobile incremented(AI). The epitome of created database is shown below:
Program: Now, nosotros will create a form for uploading images/videos files.
- HTML code:
html
<!DOCTYPE html>
<
html
>
<
head
>
<
title
>Image Upload</
championship
>
<
link
rel
=
"stylesheet"
type
=
"text/css"
href
=
"way.css"
/>
</
head
>
<
body
>
<
div
id
=
"content"
>
<
form
method
=
"Postal service"
action
=
""
enctype
=
"multipart/form-data"
>
<
input
type
=
"file"
name
=
"uploadfile"
value
=
""
/>
<
div
>
<
push
blazon
=
"submit"
name
=
"upload"
>
UPLOAD
</
push
>
</
div
>
</
grade
>
</
div
>
</
body
>
</
html
>
- CSS code: The fashion.css is the file that styles the class into a new design and the code is given below.
CSS
#content{
width
:
50%
;
margin
:
20px
car
;
border
:
1px
solid
#cbcbcb
;
}
class{
width
:
50%
;
margin
:
20px
auto
;
}
form div{
margin-acme
:
5px
;
}
#img_div{
width
:
lxxx%
;
padding
:
5px
;
margin
:
15px
auto
;
border
:
1px
solid
#cbcbcb
;
}
#img_div:afterwards{
content
:
""
;
brandish
:
block
;
clear
:
both
;
}
img{
float
:
left
;
margin
:
5px
;
width
:
300px
;
height
:
140px
;
}
You tin can copy the to a higher place code and mention it into the main code directly or create a link as same in the HTML code and attached with the master code which is given below. Every bit mentioned that if you link the stylesheet file you should create another file in .css format and saved it on the place where the main file to exist saved. The form created with the aid of POST method and the enctype="multipart/form-data is the action which encoding the files and allow you to sent through Mail.
Now we are work on the PHP code for the transfer of the image from any folder of the arrangement in a particular folder which you are mention and store it into the database as a directory.
- PHP code: The PHP lawmaking is for the uploading images, the file name is saved with the alphabetize.php, you can as well relieve with another name equally yous prefer.
php
<?php
error_reporting
(0);
?>
<?php
$msg
=
""
;
if
(isset(
$_POST
[
'upload'
])) {
$filename
=
$_FILES
[
"uploadfile"
][
"name"
];
$tempname
=
$_FILES
[
"uploadfile"
][
"tmp_name"
];
$folder
=
"paradigm/"
.
$filename
;
$db
= mysqli_connect(
"localhost"
,
"root"
,
""
,
"photos"
);
$sql
=
"INSERT INTO image (filename) VALUES ('$filename')"
;
mysqli_query(
$db
,
$sql
);
if
(move_uploaded_file(
$tempname
,
$folder
)) {
$msg
=
"Image uploaded successfully"
;
}
else
{
$msg
=
"Failed to upload image"
;
}
}
$result
= mysqli_query(
$db
,
"SELECT * FROM paradigm"
);
?>
Caption: The post-obit are the explanation to create the PHP code which is the following:
- The error_reporting(0) is for getting 0 fault while php code is running.
- $_files is work backside the scene. Information technology is being used to upload files via the HTTP Mail service method and hold the attributes of files.
- $filename is a name used to uniquely identify a computer file stored in a file system.
- $tempname is used to re-create the original proper noun of the file which is uploaded to the database as the temp name where the image is stored after upload.
- $folder defines the path of the uploaded prototype into the database to the binder where you want to exist stored. The "image/" the folder proper name where the image is to be saved after the upload. And the .$filename is used for fetching or upload the file.
- $db, the basic line for any of the PHP code for connecting to the database.
- $sql used for the inserting the image into the database of tabular array proper name image to the variable filename.
- mysqli_query is the function to executing query of $db and $sql.
- Now, permit's move the uploaded image into the folder which named every bit the prototype. The epitome named folder is saved into the WAMP or XAMPP server binder which is in C bulldoze into the www folder.
- $result office is used for the recall the image from the database.
Combination of the in a higher place codes: And the final code of upload the image into MySQL using PHP is every bit followed.
- Programme: File name: index.php This file combines the HTML and PHP lawmaking.
PHP
<?php
error_reporting
(0);
?>
<?php
$msg
=
""
;
if
(isset(
$_POST
[
'upload'
])) {
$filename
=
$_FILES
[
"uploadfile"
][
"name"
];
$tempname
=
$_FILES
[
"uploadfile"
][
"tmp_name"
];
$folder
=
"image/"
.
$filename
;
$db
= mysqli_connect(
"localhost"
,
"root"
,
""
,
"photos"
);
$sql
=
"INSERT INTO image (filename) VALUES ('$filename')"
;
mysqli_query(
$db
,
$sql
);
if
(move_uploaded_file(
$tempname
,
$folder
)) {
$msg
=
"Paradigm uploaded successfully"
;
}
else
{
$msg
=
"Failed to upload image"
;
}
}
$event
= mysqli_query(
$db
,
"SELECT * FROM image"
);
while
(
$data
= mysqli_fetch_array(
$outcome
))
{
?>
<img src=
"<?php echo $data['Filename']; ?>"
>
<?php
}
?>
<!DOCTYPE html>
<html>
<caput>
<title>Image Upload</title>
<link rel=
"stylesheet"
blazon=
"text/css"
href =
"style.css"
/>
<div id=
"content"
>
<form method=
"POST"
activeness=
""
enctype=
"multipart/form-data"
>
<input type=
"file"
name=
"uploadfile"
value=
""
/>
<div>
<push type=
"submit"
name=
"upload"
>UPLOAD</button>
</div>
</form>
</div>
</torso>
</html>
- Output: Finally, you should upload the images, videos of less than 100 MB. If you want to exceed more than change with the same.
Conclusion: The uploaded image into the database with the PHP code is having simple and using for various purposes. The code helps to upload the prototype then uploaded the image into the database and tin be shown in another folder.
One thing you should note that when you are run this program there should be a possibility that the prototype is non uploaded more than the 2 MB considering the PHP program has set the default value of uploading an epitome of 2 MB and post the image of 8 MB. For exceeding the size of uploading the paradigm you lot should follow the following steps:
- First, open the C drive, then open the folder WAMP or XAMPP server.
- Then open the bin binder.
- Open up the PHP version folder (PHP 5.6.31 folder) (KINDLY NOTE THAT IF You Accept ANOTHER VERSION OF PHP You SHOULD OPEN THAT As well)
- Then search php.ini. Open it so search the 2 variable and change with information technology. The variables are:
upload_max_size = 100M post_max_filesize = 100M
- Relieve with this change and so open
C:\wamp64\bin\apache\apache2.iv.27\bin
- and search the php.ini. Alter the same thing which are higher up mention.
- Restart the WAMP or XAMPP server and then run the code.
HTML is the foundation of webpages, is used for webpage development past structuring websites and web apps.You can acquire HTML from the ground upward by post-obit this HTML Tutorial and HTML Examples.
CSS is the foundation of webpages, is used for webpage evolution past styling websites and web apps.You tin learn CSS from the ground upward by following this CSS Tutorial and CSS Examples.
PHP is a server-side scripting language designed specifically for web development. You can acquire PHP from the footing up by following this PHP Tutorial and PHP Examples.
gallagherwasioneating.blogspot.com
Source: https://www.geeksforgeeks.org/how-to-upload-image-into-database-and-display-it-using-php/
Post a Comment for "How to Upload Image in Database Using C#"