HTML forms can also be used to get files from the user's computer in order to be uploaded on the server through a PHP script.
The HTML code for upload form field is:
<input type="file" name="field_name" />

This tutorial shows a script for uploading multiple files simultaneously to the server.
"The Secret" is the following: the value of the "name" attribute of upload fields must be the same, but with the Array type, with [] at the end of the name (like "field_name[]"), so the PHP script will receive a numeric array, named "field_name", with each element containing the data sent from one upload box. Then, the PHP script must traverse that Array to take and upload the file stored in each element, performing the necessary checks, such as size and type of each file.
Below is a script with the PHP code and a HTML form that can be used to upload multiple files on a server at the same time. Besides the basic checks (file type and size), the script has protection to awoid re-upload the files on a possible refresh.
- Other detailed explanations are added in the script code.
/*** Multi-Upload Script, from: ***/
session_start();     // Enables the possibility of working with Sessions

$updir = 'upload';     // sets the folder
$max_size = 500;       // maximum file size allowed (in KB)

// file types allowed
$allowtype = array('bmp', 'flv', 'gif', 'jpg', 'jpeg', 'mp3', 'pdf', 'png', 'rar', 'zip');

$rezultat = array();     // Variable to store the messages that will be returned by the script

// If the folder from $updir not exists, attempts to create it (with CHMOD 0777)
if (!is_dir($updir)) mkdir($updir, 0777);

/** Code for. Uploading files to server **/

// If is received a valid file from the form
if (isset($_FILES['file_up'])) {
  // Traverse the array elements, with data from the form fields with name="file_up[]"
   // Check the files received for upload
  for($f=0; $f<count($_FILES['file_up']['name']); $f++) {
    $nume_f = $_FILES['file_up']['name'][$f];     // get the name of the current file

    // if the name has minimum 4 characters
    if (strlen($nume_f)>3) {
      // get and checks the file type (its extension)
      $type = end(explode('.', strtolower($nume_f)));
      if (in_array($type, $allowtype)) {
        // check the file size
        if ($_FILES['file_up']['size'][$f]<=$max_size*1000) {
          // if there aren't errors in the copying process
          if ($_FILES['file_up']['error'][$f]==0) {
            // Set location and name for uploading file on the server
            $thefile = $updir . '/'. $nume_f;
            // if the file can't be uploaded, returns a message
            if (!move_uploaded_file ($_FILES['file_up']['tmp_name'][$f], $thefile)) {
              $rezultat[$f] = 'The file '. $nume_f. ' could not be copied, try again';
            else {
              // stores the name of the file
              $rezultat[$f] = '<b>'.$nume_f.'</b>';
        else { $rezultat[$f] = 'The file <b>'. $nume_f. '</b> exceeds the maximum permitted size, <i>'. $max_size. 'KB</i>'; }
      else { $rezultat[$f] = 'The file <b>'. $nume_f. '</b> is not an allowed file type'; }

  // To avoid resending data on refresh will do a redirect to itself, with re=up
  // Adding in a session takes messages returned by the script
  $_SESSION['rezultat'] = implode('<br />', $rezultat);
  header('Location: '. basename($_SERVER['PHP_SELF']). '?re=up');

// If $_GET['re'] and $_SESSION['rezultat'] are set, displays data from the session, then delete it
if (isset($_GET['re']) && isset($_SESSION['rezultat'])) {
  echo '<h4>Uploaded files:</h4>'. $_SESSION['rezultat'];
<!-- Form for multiple Upload fields -->
<form id="uploadform" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post" enctype="multipart/form-data">
  <input type="file" class="file_up" name="file_up[]" /><br />
  <input type="file" class="file_up" name="file_up[]" /><br />
  <input type="file" class="file_up" name="file_up[]" /><br />
  <input type="file" class="file_up" name="file_up[]" /><br />
  <input type="file" class="file_up" name="file_up[]" /><br />
  <input type="submit" value="UPLOAD" id="submit" />

To can upload the files on the server, PHP must have write permission in the directory specified at the $updir variable.
- The form from this script has five upload inputs, you can add more fields or reduce their number. This script can work with one ore multiple upload fields.

Daily Test with Code Example

Which tag defines the clickable areas inside the image map?
<map> <img> <area>
<img src="image.jpg" usemap="#map1">
<map name="map1">
  <area shape="rect" coords="9, 120, 56, 149" href="#">
  <area shape="rect" coords="100, 200, 156, 249" href="#">
Which CSS property defines what is done if the content in a box is too big for its defined space?
display overflow position
#id {
  overflow: auto;
Click on the event which is triggered when the mouse is positioned over an object.
onclick onmouseover onmouseout
document.getElementById("id").onmouseover = function(){
  document.write("Have Good Life");
Indicate the PHP variable that contains data added in URL address after the "?" character.
if(isset($_GET["id"])) {
  echo $_GET["id"];
Uploading multiple files

Last accessed pages

  1. HTML Course - Free Lessons (13288)
  2. Zodiac Signs PHP code (4129)
  3. Basic HTML elements (4078)
  4. Add data from form in text file in JSON format (11676)
  5. Contact page - CoursesWeb (33349)

Popular pages this month

  1. Making DIV Contents Scroll Horizontally, with multiple Div`s inside (1845)
  2. Contact page - CoursesWeb (1794)
  3. Tabs effect with CSS (1793)
  4. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (559)
  5. Insert, Select and Update NULL value in MySQL (442)