Today i will explain you how you can upload any file using html and php.

upload file using html and php

upload file using html and php

I have make one upload folder in which all the uploaded files will be stored. Then i am making index.html and upload.php files.

The index.html file looks like this:-

<title>Uploading files</title>

<form action="upload.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />


Note that in action i have put upload.php which i will write shortly and if you want to upload files then you must use enctype=”multipart/form-data”. Then i have inserted input field of type file and then submit button.
Now the code of upload.php file is:-


  if ($_FILES["file"]["error"] > 0)
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      echo $_FILES["file"]["name"] . " already exists. ";
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];

In this above code:-
$_FILES[“file”][“error”] is used to get errors if found any.
$_FILES[“file”][“name”] is used to get the name of the file.
$_FILES[“file”][“type”] is used to get the type of file.
$_FILES[“file”][“size”] is used to get the size of file in bytes. So i have divided this by 1024 to get the size in KB.
$_FILES[“file”][“tmp_name”] is temporary name that the uploaded file got first.
Then i am checking if the file already exists in the upload directory with the same name.
Finally I am using move_uploaded_file function to move the file to upload directory.