Assignment - 5
INTERNET WEB PROGRAMMING
(B.Tech. 3RD YEAR )
WINTER SEMESTER 2022-2023
Name SISIRA REDDY Y
Reg. No. 20BCE2448
SISIRA REDDY Y – 20BCE2448
QUESTION
Design a PHP program to get the image of the user from the HTML form used in
activity - 4. The file should be saved in your own designated folder in server. Refer file
upload concept.
Html Code:
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title> SISIRA REDDY </title>
<link rel="stylesheet" href="C:\Users\SISIRA REDDY
\OneDrive\Desktop\style 4.css">
</head>
<body>
<div class="container">
<form name="sisira" action="upload.php" onsubmit="return
validform()" method="post" encypte="multipart/form-data">
<label for="Login" class="lab" >Login</label>
<input type="text" name="Login" class="lab">
<span id="usererror" class="text-danger" font-
color:"Red"></span></br>
<label for="Password" class="lab" >Password</label>
<input type="text" name="Password" class="lab">
<span id="passworderror" class="text-danger" font-
color:"Red"></span></br>
<label for="Confirm Password" class="lab"">Confirm Password</label>
<input type="text" name="Confirm Password" class="lab">
<span id="cpassworderror" class="text-danger" font-
color:"Red"></span></br>
<label for="gender" class="lab" >Gender</label>
<select>
<option value="Male" >Male</option>
<option value="Female" >Female</option>
<option value="Others" >Others</option></select></br>
<label for="Email" class="lab" >Email</label>
<input type="text" name="Email" class="lab" ></br>
<span id="emailerror" class="text-danger" font-color:"Red"></span>
<label for="file" class="lab">File</label>
<input id="file" type="file" name="file" class="lab"></br>
<input type="submit" name="submit" value="submit"</br></form>
</div>
<script>
function validation(){
var login= document.getElementById('login').value;
var password= document.getElementById('password').value;
var cpassword= document.getElementById('Confirm Password').value;
1
SISIRA REDDY Y – 20BCE2448
var gender= document.getElementById('gender').value; var email=
document.getElementById('Email').value; var usercheck=
/^(?=.*\d).{8,}$/ ;
var passwordcheck = /^(?=.[A-Za-z])(?=.*\d)[A-Za-z\d]{16,}$/ ;
var emailcheck= /^[A-Za-z_]{3,0}?@[A-Za-z]{3,1}[.]{1}[A-Za-
z]{2,6}$/ ;
if(usercheck.test(login)){
document.getElementById('loginerror').innerHTML = " ";
}else{ document.getElementById('loginerror').innerHTML ="*"
"Username is invalid";
return false;
}
if(passwordcheck.test(password)){
document.getElementById('passworderror').innerHTML = " ";
}else{ document.getElementById('passworderror').innerHTML ="*"
"Password is invalid";
return false;
}
if(password.match(cpassword)){
document.getElementById('cpassworderror').innerHTML = " ";
}else{
alert("entered password is not the same");
document.getElementById('cpassworderror').innerHTML ="*" "Password
is not matching";
return false;
}
if(emailcheck.test(email)){
document.getElementById('emailerror').innerHTML = " ";
}else{ document.getElementById('emailerror').innerHTML ="*" "Email
is invalid";
return false;
}
}
</script>
</body>
</html>
CSS Code:
input:valid
{
background-color: LightGreen
}
input:invalid
{
background-color: Yellow
}
select:valid
{
2
SISIRA REDDY Y – 20BCE2448
background-color: LightGreen
}
select:invalid
{
background-color: Yellow
}
.container
{
background-color: grey; width: 500px;
padding: 25px;
border: 25px solid black; margin: auto;
}
label{ color:white; text-align: left;
display:inline-block; margin-left:10px;
}
.lab{ width:150px;
margin-bottom:10px; padding:5px;
} #d1{
width:20%; border:10px white;
background-color:white;
text-color:black;
}
PHP Code:
<?php if(isset($_POST['submit'])){
$file=$_FILES['file'];
$filename = $_FILES['file']['name'];
$filetmpname = $_FILES['file']['tmp_name'];
$filesize = $_FILES['file']['size'];
$fileerror = $_FILES['file']['error'];
$filetype = $_FILES['file']['type'];
$fileExt = explode('.',$filename);
$fileactualext = strtolower(end($fileext));
$allowed = array('jpg','jpeg','png','pdf');
if(in_array($fileactualext, $allowed)) { if ($file_error == 0){
if ($filesize < 1000000) {
$filenamenew = uniqid('',true).".".$fileactualext;
$filedestination = 'upload/'.$filenamenew; move_upload_file(
$filetmpname , $filedestination); echo “File is an image”;
}else {
echo "your file is big";
}
}else {
echo "There was an error uploading your file!";
}
} else {
echo "You cannot upload files of this type";
}
3
SISIRA REDDY Y – 20BCE2448
}
?>
Output:
For Basic Code
Html Code:
<!DOCTYPE html>
<html>
<head>
<title>Upload Image Form</title>
</head>
<body>
<h2>Upload Image Form</h2>
<form action="upload_image.php" method="POST"
enctype="multipart/form-data">
<label for="image">Select Image:</label>
<input type="file" name="image" id="image"><br><br>
<input type="submit" value="Upload">
</form>
</body>
</html>
PHP Code:
<?php
// Define the folder where the image will be saved
$target_dir = "uploads/";
// Get the file name and extension of the uploaded image
4
SISIRA REDDY Y – 20BCE2448
$file_name = basename($_FILES["image"]["name"]);
$file_ext = pathinfo($file_name, PATHINFO_EXTENSION);
// Generate a unique name for the image to avoid overwriting
existing files
$unique_name = uniqid() . '.' . $file_ext;
// Set the target path for the uploaded image
$target_path = $target_dir . $unique_name;
// Check if the file was uploaded successfully
if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_path))
{
// Display a success message with the path to the uploaded
image
echo "The file ". $unique_name. " has been uploaded to " .
$target_path;
} else {
// Display an error message if the file upload failed
echo "Sorry, there was an error uploading your file.";
}
?>
The user can select an image file using an HTML form and submit it to the PHP script
(upload_image.php) for processing. The PHP script saves the image file in a designated folder
on the server and generates a unique name for the file to avoid overwriting existing files. If
the upload is successful, a success message is displayed with the path to the uploaded image.
If there is an error, an error message is displayed instead.
Note: Make sure to create the "uploads" folder in the same directory as the PHP script and set
its permissions to allow file uploads.