PHP

Upload a file using PHP

Pinterest LinkedIn Tumblr

First, write your HTML code like this, and don’t forget to add 

enctype="multipart/form-data"
<form action="upload.php" method="post" enctype="multipart/form-data">
  <input type="file" name="fileToUpload" id="fileToUpload">
  <input type="submit" value="Upload Image" name="submit">
</form>

Then create a file name called upload.php

<?php
$path = "form/";
$target_file =  $path.basename($_FILES["fileToUpload"]["name"]);
$file=$_FILES['fileToUpload']['name'];    
$result = move_uploaded_file($_FILES['fileToUpload']['tmp_name'],$target_file.$file);
if ($result) {
    echo "file successfully uploaded";
}
else {
    echo "please select your file";
}

Write A Comment