IWP Activity-5
IWP Activity-5
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
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:
PHP Code:
<?php
4
SISIRA REDDY Y – 20BCE2448
$file_name = basename($_FILES["image"]["name"]);
$file_ext = pathinfo($file_name, PATHINFO_EXTENSION);
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.