File Upload in Php

File Upload in Php

File Upload in PHP is mainly used in registration forms like student registration, employee registration. In this registration form, we see resume upload, signature upload, document upload. To do this kind of work, we use PHP.

 

HTML Code :

<form method=”post” enctype=”multipart/form-data”>
    <div class="form-group">
      <label for="email">upload image:</label>
      <input type="file" class="form-control" name="doc">
    </div>
    <button type="submit" name=”submit” class="btn btn-primary">Submit</button>
  </form>

 

In this enctype=”multipart/form-data” it is mandatory to inform when we want to upload the file or work with file input. If we don’t use it then our PHP code will not work, so keep in mind this point before using file input in the form.

 

 

PHP Code :

<?php
if(isset($_POST[‘submit’]))
{
    $filename = $_FILES[‘doc’][‘name’];
    $tempfile = $_FILES[‘doc’][‘tmp_name’];
    move_uploaded_file($tempfile,"images/".$filename);
}

?php> 

Here in this code

move_uploaded_file(): is a PHP function used to move your image from a temporary location to a central location where you want to upload your file.

 

Complete Code :

<?php
 
if(isset($_POST['submit']))
{
 
 
$file_name = $_FILES['doc']['name'];
$file_tmp =$_FILES['doc']['tmp_name'];
move_uploaded_file($file_tmp,"doc/".$file_name);
 
}
 
 
?>
 
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</head>
<body>
 
<div class="container">
<div class="row">
<div class="col-lg-12">
 
<form method="post" enctype="multipart/form-data">
  <div class="form-group">
    <label for="email">Document:</label>
    <input type="file" class="form-control" name="doc">
  </div>
 
 
  <button type="submit" name="submit" class="btn btn-primary">Submit</button>
</form>
 
</div>
</div>
</div>
 
</body>
</html>

Categories: web development php

Trending Courses

CodeIgniter

Regular : 45 Days

Fastrack : 20 Days

Crash : 10 Days

Advance Digital Marketing Expert Course

Regular : 6 Months

Fastrack : 3 Months

Crash : 2 Months

React JS

Regular : 45 Days

Fastrack : 25 Days

Crash : 15 Days

Laravel

Regular : 45 Days

Fastrack : 20 Days

Crash : 10 Days

Front End Developer

Regular : 6 Months

Fastrack : 4 Months

Crash : 2 Months

Web Expert with Python

Regular : 12 Months

Fastrack : 6 Months

Crash : 3 Months

Related Blogs

Request For Demo