Upload Files to Sql Database Using C# Mvc
Uploading the image/videos into the database and display it using PHP is the way of uploading the image into the database and fetched it from the database. Using the PHP code, the user uploads the image or videos they are safely getting entry into the database and the images should be saved into a particular location by fetching these images from the database.
If any of the websites contain the functionality to upload images/videos with some item, then past using this code we volition upload the image into your database and whether yous would like to ascertain what the person has got to be uploaded. And by this lawmaking the image which is uploaded that where save in your organisation where you are given the location.
First, create the database on XAMPP/WAMP server using phpMyAdmin and give the database proper noun is photos and the table proper name is image. The table contains two fields:
- Id – int(eleven)
- Filename – VARCHAR(100)
Id should be in Car incremented(AI). The epitome of created database is shown below:
Programme: Now, we will create a form for uploading images/videos files.
- HTML lawmaking:
html
<!DOCTYPE html>
<
html
>
<
head
>
<
title
>Image Upload</
championship
>
<
link
rel
=
"stylesheet"
type
=
"text/css"
href
=
"style.css"
/>
</
head
>
<
trunk
>
<
div
id
=
"content"
>
<
form
method
=
"Mail"
action
=
""
enctype
=
"multipart/form-data"
>
<
input
type
=
"file"
name
=
"uploadfile"
value
=
""
/>
<
div
>
<
button
type
=
"submit"
name
=
"upload"
>
UPLOAD
</
push button
>
</
div
>
</
grade
>
</
div
>
</
body
>
</
html
>
- CSS code: The style.css is the file that styles the class into a new design and the code is given beneath.
CSS
#content{
width
:
50%
;
margin
:
20px
car
;
border
:
1px
solid
#cbcbcb
;
}
form{
width
:
50%
;
margin
:
20px
auto
;
}
form div{
margin-top
:
5px
;
}
#img_div{
width
:
lxxx%
;
padding
:
5px
;
margin
:
15px
car
;
border
:
1px
solid
#cbcbcb
;
}
#img_div:after{
content
:
""
;
brandish
:
cake
;
clear
:
both
;
}
img{
float
:
left
;
margin
:
5px
;
width
:
300px
;
peak
:
140px
;
}
Yous tin copy the above code and mention information technology into the main code directly or create a link as aforementioned in the HTML lawmaking and fastened with the main code which is given below. As mentioned that if you link the stylesheet file you should create some other file in .css format and saved it on the place where the principal file to exist saved. The grade created with the help of POST method and the enctype="multipart/form-data is the activity which encoding the files and allow you to sent through Mail service.
Now we are work on the PHP code for the transfer of the image from any folder of the system in a particular folder which you are mention and store it into the database as a directory.
- PHP code: The PHP code is for the uploading images, the file name is saved with the index.php, you lot tin can also save with another proper name every bit y'all adopt.
php
<?php
error_reporting
(0);
?>
<?php
$msg
=
""
;
if
(isset(
$_POST
[
'upload'
])) {
$filename
=
$_FILES
[
"uploadfile"
][
"name"
];
$tempname
=
$_FILES
[
"uploadfile"
][
"tmp_name"
];
$folder
=
"epitome/"
.
$filename
;
$db
= mysqli_connect(
"localhost"
,
"root"
,
""
,
"photos"
);
$sql
=
"INSERT INTO prototype (filename) VALUES ('$filename')"
;
mysqli_query(
$db
,
$sql
);
if
(move_uploaded_file(
$tempname
,
$binder
)) {
$msg
=
"Epitome uploaded successfully"
;
}
else
{
$msg
=
"Failed to upload paradigm"
;
}
}
$result
= mysqli_query(
$db
,
"SELECT * FROM prototype"
);
?>
Explanation: The following are the explanation to create the PHP code which is the post-obit:
- The error_reporting(0) is for getting 0 mistake while php code is running.
- $_files is work behind the scene. It is being used to upload files via the HTTP Mail method and hold the attributes of files.
- $filename is a proper name used to uniquely identify a computer file stored in a file organization.
- $tempname is used to copy the original name 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 epitome into the database to the folder where you want to be stored. The "paradigm/" the binder proper name where the image is to exist saved after the upload. And the .$filename is used for fetching or upload the file.
- $db, the basic line for whatsoever of the PHP code for connecting to the database.
- $sql used for the inserting the image into the database of table name epitome to the variable filename.
- mysqli_query is the part to executing query of $db and $sql.
- At present, let'south move the uploaded epitome into the folder which named every bit the image. The paradigm named folder is saved into the WAMP or XAMPP server binder which is in C drive into the www folder.
- $result office is used for the call up the paradigm from the database.
Combination of the in a higher place codes: And the terminal code of upload the image into MySQL using PHP is as followed.
- Program: File name: alphabetize.php This file combines the HTML and PHP code.
PHP
<?php
error_reporting
(0);
?>
<?php
$msg
=
""
;
if
(isset(
$_POST
[
'upload'
])) {
$filename
=
$_FILES
[
"uploadfile"
][
"proper name"
];
$tempname
=
$_FILES
[
"uploadfile"
][
"tmp_name"
];
$binder
=
"paradigm/"
.
$filename
;
$db
= mysqli_connect(
"localhost"
,
"root"
,
""
,
"photos"
);
$sql
=
"INSERT INTO image (filename) VALUES ('$filename')"
;
mysqli_query(
$db
,
$sql
);
if
(move_uploaded_file(
$tempname
,
$binder
)) {
$msg
=
"Image uploaded successfully"
;
}
else
{
$msg
=
"Failed to upload prototype"
;
}
}
$event
= mysqli_query(
$db
,
"SELECT * FROM prototype"
);
while
(
$data
= mysqli_fetch_array(
$result
))
{
?>
<img src=
"<?php echo $information['Filename']; ?>"
>
<?php
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Image Upload</title>
<link rel=
"stylesheet"
type=
"text/css"
href =
"fashion.css"
/>
<div id=
"content"
>
<form method=
"Mail service"
activeness=
""
enctype=
"multipart/grade-information"
>
<input type=
"file"
name=
"uploadfile"
value=
""
/>
<div>
<button type=
"submit"
name=
"upload"
>UPLOAD</push>
</div>
</form>
</div>
</body>
</html>
- Output: Finally, you lot should upload the images, videos of less than 100 MB. If you want to exceed more than than change with the same.
Conclusion: The uploaded image into the database with the PHP code is having unproblematic and using for diverse purposes. The code helps to upload the paradigm and then uploaded the image into the database and tin can be shown in another folder.
Ane thing yous should note that when you lot are run this program there should be a possibility that the image is non uploaded more the 2 MB because the PHP program has set the default value of uploading an image of 2 MB and post the epitome of 8 MB. For exceeding the size of uploading the image you should follow the following steps:
- First, open the C drive, then open up the folder WAMP or XAMPP server.
- So open the bin folder.
- Open up the PHP version folder (PHP 5.6.31 folder) (KINDLY Notation THAT IF Y'all HAVE ANOTHER VERSION OF PHP YOU SHOULD OPEN THAT As well)
- So search php.ini. Open it and so search the two variable and change with information technology. The variables are:
upload_max_size = 100M post_max_filesize = 100M
- Salve with this modify then open up
C:\wamp64\bin\apache\apache2.4.27\bin
- and search the php.ini. Change the same thing which are above mention.
- Restart the WAMP or XAMPP server then run the code.
HTML is the foundation of webpages, is used for webpage evolution by structuring websites and web apps.You can learn HTML from the basis up past following this HTML Tutorial and HTML Examples.
CSS is the foundation of webpages, is used for webpage development by styling websites and web apps.You can learn CSS from the ground up by following this CSS Tutorial and CSS Examples.
PHP is a server-side scripting language designed specifically for spider web development. Yous can larn PHP from the ground up by post-obit this PHP Tutorial and PHP Examples.
rossiforgerbours1990.blogspot.com
Source: https://www.geeksforgeeks.org/how-to-upload-image-into-database-and-display-it-using-php/
Post a Comment for "Upload Files to Sql Database Using C# Mvc"