Monday, August 2, 2010

Dynamic Multiple File Uploads in PHP

I have a PHP script here which will let you specify the folder name and the number of files to be uploaded. Here is the preview.



This program has basic form validation and notifications on entering the data for the file uploads. You are free to copy the code and customize it the way you want it to be. I also provided comments in every lines of code for your guide in tracing or debugging.


<?php

// display open div wrapper
echo "<div class='wrapper'>";
// form for folder name and number of files
$num_form = "<form method=post>
    <table border='0' width='400' cellspacing='0' cellpadding='3' align=center>
    <tr><td>Folder Name</td><td>
    <input type=text name='fname' class='bginput'></td></tr>
    <tr><td>Number of Files</td><td>
    <input type=text name='num_imgs' class='bginput'></td></tr>
    <tr><td><input type=submit name='go' value='GO »' /></td></tr>
    </form>";


// check if the form for folder name and number of files was submitted
if(isset($_POST['go'])){
    //check if both fields were provided data
    if($_POST['fname']=='' || $_POST['num_imgs']==''){
       // notify the data error
       echo "<div class='notify'>Please provide data on both fields.</div>" . $num_form;
    }
    // check if the number of files is valid
    else if(number_format((int) $_POST['num_imgs']) == 0){
       // notify invalid number errors
       echo "<div class='notify'>Invalid number of images inputted.</div>" . $num_form;
    }
    else{
       // get the current directory path
       $thisdir = getcwd();
       // check if the directory already exists
       if(!is_dir($_POST['fname'])){
          // create the directory and change the file permissions
          mkdir(($thisdir.'/'.$_POST['fname']) , 0777);
}
       // form for file uploads
       $form = "<form method=post enctype='multipart/form-data'>";
       $form .= "<table border='0' width='400' cellspacing='0' cellpadding='0' align=center>";
       // input fields depending on the number of files entered
       for($i=1; $i<=$_POST['num_imgs']; $i++){
          $form .= "<tr><td>Image $i</td><td>
          <input type=file name='images[]' class='bginput'></td></tr>";
       }
       $form .= "<tr><td colspan=2 align=center><input type=submit name='add_image' value='Upload Files'></td></tr>";
       $form .= "<input type='hidden' name='fname' value=".$_POST['fname']." />";
       $form .= "</form> </table>";
       // display form file uploads
      echo $form;
    }
}
// check if form file uploads was submitted
else if(isset($_POST['add_image'])){
    // check file name was set
    if(isset($_FILES['images']['name'])){
       // counter for files uploaded
       $ctr = 0;
       // loop through all files to be uploaded
       while(list($key,$value) = each($_FILES['images']['name'])){
          // check if any blank field is entered
          if(!empty($value)){
             // increment counter
             $ctr++;
             // filename stores the value
             $filename = $value;
             // replace blank space with _
             $filename=str_replace(" ","_",$filename);
             //folder name
             $foldername = $_POST['fname'];
             // set upload directory path
             $add = "$foldername/$filename";
             // upload the file to the server
             copy($_FILES['images']['tmp_name'][$key], $add);
          }
       }
       // notify successful uploads
       echo "<div class='notify'>You have successfully uploaded $ctr file(s).</div>";
       echo "<br/><a href='multiple-uploads.php'><button>Upload more files »</button></a>";
    }
}
else{
    // display form for folder name and number of files
    echo $num_form;
}
// display close div wrapper
echo "</div";

?>


I have also here the CSS code for basic formatting of the program.


.wrapper {
    font-family:Verdana, Geneva, sans-serif;
    font-size:12px;
    width:420px;
    margin:0 auto;
    padding: 10px;
    background:#f4f4f4;
}

.notify{
    padding:2px 10px;
    background:#fff;
}

No comments:

Post a Comment