Php File Upload With Validation In Hindi Tutorial || Php File Upload



Php File Upload With Validation In Hindi Tutorial || Php File Upload



 Index.html 


<?php

include('upload.php');

?>

<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

<title>Php File Upload</title>
</head>
<body>
<div class="container ">
<h1 class="mt-5 text-center">PHP File Upload</h1>
<br>
<form action="" enctype="multipart/form-data" method="post">
<input type="file" class="form-control" name="file">
<br>
<input type="submit" class="btn btn-primary" value="Upload" name="btn">
</form>
<br>
<?php
if(isset($err)){
?>
<div class="alert alert-danger"><?php echo $err; ?></div>
<?php
}
if(isset($ses)){
?>
<div class="alert alert-success"><?php echo $ses; ?></div>
<br>
<img src="<?php echo $dir; ?>" width="500px">
<?php
}
?>
</div>
</body>
</html>





Upload.php


<?php

if(isset($_POST['btn'])){
$file = $_FILES['file'];
$file_size = $file['size'];
$file_name = $file['name'];
// print_r($file);
$tmp = $file['tmp_name'];
$pathexe = pathinfo($file_name,PATHINFO_EXTENSION);
$ext = strtolower($pathexe);
$acext = array('jpg','png','jpeg','gif');


if($file_size == 0)
{
$err = 'Select A image File';
return false;
}
<?php
if($file_size > 500000)
{
$err = 'Your FIle size Too Long';
return false;
}

if (!in_array($ext,$acext)) {
$err = 'invalid file Type';
return false;
}

$dir = 'uploads/'.rand().".".$ext;

$upld = move_uploaded_file($tmp,$dir);

if($upld){
$ses = 'File Uploaded';
}else{
$err = 'file Not Upload';
}

}

Post a Comment

Previous Post Next Post