Breaking News
You are here: Home / Notes / SEM-5 / PHP MYSQL / File Upload in php

File Upload in php

File Upload

  • Form tag plays very important role in file uploading.
  • The most important about the form is that you should make it uses POST and not GET method and also you should use enctype=”multipart/form-data” so it knows that a file will be transferred.
  • Now we need to make a folder (names ‘uploading’) on our web server and set its permissions to 777 so the upload.php script that we will do is able to write the file in it.



<h2>file upload with php</h2>

<form action="upload.php" method="post" enctype="multipart/form-data">

Filename:<input type="file" name="file" id="file" /><br />

<input type="submit" name="submit" value="upload file" />



  • The enctype attribute of the <form> tag specifies which content-type to use when submitting the form. “multipart/form-data” is used when a form requires binary data, like the contents of a file, to be uploaded

 Restrict an upload file

In this script we add some restrictions to the file upload. The user may only upload .gif or .jpeg files and the file size must be under 20 kb:

if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/bmp")

|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 10000))
  if ($_FILES["file"]["error"] > 0)
    echo “Error”;

    move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],"upload/" . $_FILES["fileToUpload"]["name"]);





  echo "Invalid file type";


File upload code


$folder = “test/”;

if (is_uploaded_file($HTTP_POST_FILES['filename']['tmp_name']))  {  

    if (move_uploaded_file($HTTP_POST_FILES['filename']['tmp_name'], $folder.$HTTP_POST_FILES['filename']['name'])) {

         Echo “File uploaded”;

    } else {

         Echo “File not moved to destination folder. Check permissions”;


} else {

     Echo “File is not uploaded.”;




How To Download the Uploaded File


$dir = "upload/";


$filename = $dir.$file;



      header('Content-Description: File Transfer');

      header('Content-Type: application/octet-stream');

      header('Content-Disposition: attachment; filename='.basename($filename));

      header('Content-Transfer-Encoding: binary');

      header('Expires: 0');

      header('Cache-Control: must-revalidate, post-check=0, pre-check=0');

      header('Pragma: public');

      header('Content-Length: ' . filesize($filename));








Download Paper Solutions from Below Link

About admin

Scroll To Top