PHP – Simple File Uploader

With PHP you can upload file inside a webserver using a HTML Form.

DOWNLOAD

 

WARNING!!!

1. Providers allow the upload of file in shared web space with restrictions of:

– Type (example: .zip files are allowed, .exe files are not allowed)
– Size
– Only inside special folders (example: public)

You must FIRST verify your hosting contract!

2. The upload folder must have write/read permission (755)
To Change folders permissions you can use an FTP software or the Admin Panel of your Web Site.

You must create in the same folder:

1. upload-form.php -> the HTML upload form
2. upload-engine.php -> the PHP uploader engine
3. a folder ‘upload’ -> the upload folder

upload-form.php

<html>
<body>

<!-- Upload Form START -->
<!-- Notice the enctype type is 'multipart/form-data' It means a bynary data -->
<form action="upload-engine.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">
</form>
<!-- Upload Form END -->

</body>
</html>

upload-engine.php

<?php
if ($_FILES["file"]["error"] > 0) // the error code resulting from the file upload
  {
  // If there is an error give a message
  echo "Error: " . $_FILES["file"]["error"] . "<br>";
  }
else
  {
  // the name of the uploaded file
  echo "Upload: " . $_FILES["file"]["name"] . "<br>"; 
  // the type of the uploaded file  
  echo "Type: " . $_FILES["file"]["type"] . "<br>"; 
  // the size in Kbytes of the uploaded file  
  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>"; 
  // the name of the temporary copy of the file stored on the server
  echo "Stored in: " . $_FILES["file"]["tmp_name"];             
  }
?>

Output:

Upload: 1.jpg
Type: image/jpeg
Size: 16.1640625 kB
Stored in: /var/tmp/php1HXXSm

Temporary Copy -> Save Permanent Copy

The examples above create a temporary copy of the uploaded files in the PHP temp folder on the server.

The temporary copied files disappears when the script ends.

To store the uploaded file we need to copy it to a different location.

upload-engine.php

<?php
if ($_FILES["file"]["error"] > 0) // the error code resulting from the file upload
  {
  // If there is an error give a message
  echo "Error: " . $_FILES["file"]["error"] . "<br>";
  }
else
  {
  // the name of the uploaded file
  echo "Upload: " . $_FILES["file"]["name"] . "<br>"; 
  // the type of the uploaded file  
  echo "Type: " . $_FILES["file"]["type"] . "<br>"; 
  // the size in Kbytes of the uploaded file  
  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>"; 
  // the name of the temporary copy of the file stored on the server
  echo "Stored in: " . $_FILES["file"]["tmp_name"];  

  // Muovi il file con il nome temporaneo -> dentro la cartella upload -> salvalo con il suo nome originale
  // NOTICE: se il file esiste già verrà sovrascritto!!!
  move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $_FILES["file"]["name"]);
  echo "<br> Stored in: " . "upload/" . $_FILES["file"]["name"];  
  }
?>

NOTICE:
the PHP function – move_uploaded_file() –

Output:

Upload: 1.jpg
Type: image/jpeg
Size: 16.1640625 kB
Stored in: /var/tmp/phpQy4CY8
Stored in: upload/1.jpg

Temporary Copy -> Change Name -> Save Permanent Copy

upload-engine.php

<?php
if ($_FILES["file"]["error"] > 0) // the error code resulting from the file upload
  {
  // If there is an error give a message
  echo "Error: " . $_FILES["file"]["error"] . "<br>";
  }
else
  {
  // the name of the uploaded file
  echo "Upload: " . $_FILES["file"]["name"] . "<br>"; 
  // the type of the uploaded file  
  echo "Type: " . $_FILES["file"]["type"] . "<br>"; 
  // the size in Kbytes of the uploaded file  
  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>"; 
  // the name of the temporary copy of the file stored on the server
  echo "Stored in: " . $_FILES["file"]["tmp_name"];  

  $uploads_dir = 'upload/'; // upload folder
  $newname = 'newname.jpg'; // upload new name
  // Muovi il file con il nome temporaneo -> dentro la cartella upload -> salvalo con il suo nuovo nome
  // NOTICE: se il file esiste già verrà sovrascritto!!!
  move_uploaded_file($_FILES["file"]["tmp_name"],"$uploads_dir"."$newname");
  echo "<br> Stored in: " . $uploads_dir . $newname; 
  }
?>

Output:

Upload: 3.jpg
Type: image/jpeg
Size: 14.541015625 kB
Stored in: /var/tmp/php77QJJH
Stored in: upload/newname.jpg

Upload Restrictions

<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");

$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000) // size restriction
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
  else
    {
    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. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>