if you want to upload images on the server using PHP then you should should follow me ...
step 1 > Create a form and with a submit button in 'index.html'.
step2 > Create a folder 'upload' in the same directory where your 'index.html' located.
step3 > 'show.php' to show all the images from your server.
step1 > index.php
<!DOCTYPE html>
<html>
<head>
<title>Upload photo</title>
</head>
<body>
<h2>Choose file to upload ...</h2>
<form action="process.php" method="POST" enctype="multipart/form-data">
choose file <input type="file" name="filename">
<input type="submit" value="Submit"><br><br>
</form>
<form action="show.php" method="POST" enctype="multipart/form-data">
<button type="submit">Show Pics</button>
</form>
</body>
</html>
------------------------------------------------------------------------------------------------------------
Server side ..........................
-------------------------------------------------------------------------------------------------------------------
Step 2 > process.php
<?php
$con = new mysqli("localhost",'root','','image_test');
if(!$con){
die("Error :".mysqli_error($con));
}
$name = $_FILES['filename']['name'];
$target_dir = 'upload/';
$target_file = $target_dir.basename($_FILES['filename']['name']);
//select file type
$imagefiletype = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
//valid file extentions
$extentions_arr = array("jpg",'png','jpeg','gif');
//check extention
if(in_array($imagefiletype, $extentions_arr)){
//insert record
$query = mysqli_query($con,"INSERT INTO images(name) VALUES ('$name')");
//upload file
if(move_uploaded_file($_FILES['filename']['tmp_name'], $target_file)){
echo "Successfully uploaded......";
}else{
echo "Try again!!!";
}
}else{
echo "Please upload image with 'jpg','png','jpeg','gif' format";
}
?>
Step 3 > show.php
<?php
$con = new mysqli("avatar.com",'root','','image_test');
if(!$con){
die("Error :".mysqli_error($con));
}
$sql = mysqli_query($con,'SELECT name FROM images');
while($row = mysqli_fetch_array($sql)){
$image = $row['name'];
$image_src = "upload/".$image;
echo "<img src='$image_src' width='200px' height='200px'/>";
}
?>
Save these files in your directory and don't forget 'upload' folder in the same directory.
step 1 > Create a form and with a submit button in 'index.html'.
step2 > Create a folder 'upload' in the same directory where your 'index.html' located.
step3 > 'show.php' to show all the images from your server.
step1 > index.php
<!DOCTYPE html>
<html>
<head>
<title>Upload photo</title>
</head>
<body>
<h2>Choose file to upload ...</h2>
<form action="process.php" method="POST" enctype="multipart/form-data">
choose file <input type="file" name="filename">
<input type="submit" value="Submit"><br><br>
</form>
<form action="show.php" method="POST" enctype="multipart/form-data">
<button type="submit">Show Pics</button>
</form>
</body>
</html>
------------------------------------------------------------------------------------------------------------
Server side ..........................
CREATE TABLE `images` ( `id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT, `name` varchar(200) NOT NULL, `image` longtext NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-------------------------------------------------------------------------------------------------------------------
Step 2 > process.php
<?php
$con = new mysqli("localhost",'root','','image_test');
if(!$con){
die("Error :".mysqli_error($con));
}
$name = $_FILES['filename']['name'];
$target_dir = 'upload/';
$target_file = $target_dir.basename($_FILES['filename']['name']);
//select file type
$imagefiletype = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
//valid file extentions
$extentions_arr = array("jpg",'png','jpeg','gif');
//check extention
if(in_array($imagefiletype, $extentions_arr)){
//insert record
$query = mysqli_query($con,"INSERT INTO images(name) VALUES ('$name')");
//upload file
if(move_uploaded_file($_FILES['filename']['tmp_name'], $target_file)){
echo "Successfully uploaded......";
}else{
echo "Try again!!!";
}
}else{
echo "Please upload image with 'jpg','png','jpeg','gif' format";
}
?>
Step 3 > show.php
<?php
$con = new mysqli("avatar.com",'root','','image_test');
if(!$con){
die("Error :".mysqli_error($con));
}
$sql = mysqli_query($con,'SELECT name FROM images');
while($row = mysqli_fetch_array($sql)){
$image = $row['name'];
$image_src = "upload/".$image;
echo "<img src='$image_src' width='200px' height='200px'/>";
}
?>
Save these files in your directory and don't forget 'upload' folder in the same directory.
Comments
Post a Comment