<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>File - Upload</title>
<style type="text/css">
<!--
#Layer3 {
        position:absolute;
        left:150px;
        top:100px;
        width:700px;
        height:400px;
}
.style4 {
        font-family: Arial, Helvetica, sans-serif;
        font-size: 22px;
        font-weight: bold;
}
.style5 {font-size: 16px}
-->
</style>
</head>

<body bgcolor="lightyellow">
<div id="Layer3">
<span class="style4">Upload Files</span>
<?php
    error_reporting(0);

    if($_POST)
        validate();

    display();
//------------------------------------------------------------------------------------------------------
function validate()
{
        global $client_filename, $server_filename, $directory;
        global $client_fname_error, $server_fname_error, $server_dir_error, $check, $msg, $msg2;

        $client_tmpname1   = $_FILES['client_filename1']['tmp_name'];
        $client_tmpname2   = $_FILES['client_filename2']['tmp_name'];
        $client_filename1  = $_FILES['client_filename1']['name'];
        $client_filename2  = $_FILES['client_filename2']['name'];
        $server_filename1  = $_POST['server_filename1'];
        $server_filename2  = $_POST['server_filename2'];
        $server_directory1 = $_POST['server_directory1'];
        $server_directory2 = $_POST['server_directory2'];

        $client_fname_error;
        $server_fname_error;
        $server_dir_error;
        $check;
        $msg;

        if ($client_filename1 == '' && $client_filename2 == '') {
                $client_fname_error = '*';
                $check = 'error';
        }
        if ($server_filename1 == '' && $server_filename2 == '') {
                $server_fname_error = '*';
                $check = 'error';
        }       
        if ($server_directory1 =='' && $server_directory2 =='') {
                $server_dir_error = '*';
                $check = 'error';
        }
        if ($check == 'error') {
                $msg = 'Please enter missing data!';
                return;
        }

        $return_code = $_FILES['client_filename1']['error'];
        if ($client_filename1 == '')                            //no upload is requested 
            $return_code = 0; 

        switch ($return_code)
        {
                case 1: $msg = 'File exceeded upload_max_filesize in php.ini'; break;
                case 2: $msg = 'File exceeded max_file_size     in html form'; break;
                case 3: $msg = 'File only partially uploaded';                 break;
                case 4: $msg = 'No file uploaded';                             break;
                case 6: $msg = 'No temporary directory to store file';         break;
                case 7: $msg = 'Failed to write file to server disk';          break;
                case 8: $msg = 'Upload stopped by extension';                  break;
                case 6: $msg = 'Failed to write file to server disk';          break;
        }

        $return_code = $_FILES['client_filename2']['error']; 
        if ($client_filename2 == '')                            //no upload is requested 
            $return_code = 0; 

        switch ($return_code)
        {
                case 1: $msg = 'File exceeded upload_max_filesize in php.ini'; break;
                case 2: $msg = 'File exceeded max_file_size     in html form'; break;
                case 3: $msg = 'File only partially uploaded';                 break;
                case 4: $msg = 'No file uploaded';                             break;
                case 6: $msg = 'No temporary directory to store file';         break;
                case 7: $msg = 'Failed to write file to server disk';          break;
                case 8: $msg = 'Upload stopped by extension';                  break;
                case 6: $msg = 'Failed to write file to server disk';          break;
        }

        if ($msg) return;
        
        if ( is_uploaded_file( $client_tmpname1 ))
        {
            $server_dir_file1 = $server_directory1 . '/' . $server_filename1;
            $ok =  move_uploaded_file( $client_tmpname1, $server_dir_file1 );
            if (! $ok)
                $msg = $client_filename1 . " Not Uploaded - Directory not found";

        }
        if ( is_uploaded_file( $client_tmpname2 ))
        {
            $server_dir_file2 = $server_directory2 . '/' . $server_filename2;
            $ok = move_uploaded_file( $client_tmpname2, $server_dir_file2 );
            if (! $ok)
                $msg = $client_filename2 . " Not Uploaded - Directory not found";
        }

        if ($msg) return;

        $msg   = "Your file(s) have been uploaded successfully!";

        $msg2  = "<br>File name:   " . $_FILES['client_filename1']['name'];
        $msg2 .= "<br>File type:   " . $_FILES['client_filename1']['type'];
        $msg2 .= "<br>File size:   " . $_FILES['client_filename1']['size'];
        $msg2 .= "<br>Temp name:   " . $_FILES['client_filename1']['tmp_name'];
        $msg2 .= "<br>New name:    " . $server_dir_file1;
        $msg2 .= "<br>Return code: " . $_FILES['client_filename1']['error'];
        $msg2 .= "<br>";
        $msg2 .= "<br>File name:   " . $_FILES['client_filename2']['name'];
        $msg2 .= "<br>File type:   " . $_FILES['client_filename2']['type'];
        $msg2 .= "<br>File size:   " . $_FILES['client_filename2']['size'];
        $msg2 .= "<br>Temp name:   " . $_FILES['client_filename2']['tmp_name'];
        $msg2 .= "<br>New name:    " . $server_dir_file2;
        $msg2 .= "<br>Return code: " . $_FILES['client_filename2']['error'];
}

//------------------------------------------------------------------------------------------------------
function display()
{       
        global $client_filename1, $server_filename1, $server_directory1;
        global $client_filename2, $server_filename2, $server_directory2;
        global $client_fname_error, $server_fname_error, $server_dir_error, $check, $msg, $msg2;
        
        print "<form METHOD=post ACTION=upload2.php ENCTYPE=multipart/form-data> \n";    
        print "<fieldset>";
        print "<legend><h3>Please enter at least one row</h3></legend>";
        print "<table border='0'> \n";
        print "<colgroup span=1 width=170 /> \n";
        print "<!-- MAX_FILE_SIZE hidden field must come before input type=file --> \n";
        print "<input type=hidden name=MAX_FILE_SIZE value=5000000 /> \n";  
        print "<tr>";
        print "<td>Choose a local file   <font color='red'>$client_fname_error </font></td>";
        print "<td>Name of file on server<font color='red'>$server_fname_error </font></td>";
        print "<td>Name of directory     <font color='red'>$server_dir_error   </font></td>";
        print "</tr>";
        print "<tr>";
        print "<td><input type='file' size=30 name='client_filename1' /></td> \n";
        print "<td><input type='text' size=30 name='server_filename1' /></td> \n";
        print "<td><select name='server_directory1' size='1'> \n";      
        print "<option value='upload'>        Upload  </option> \n";
        print "<option value='upload_image'>  Images   </option> \n";
        print "<option value='upload_other' > Other   </option> \n";
        print "</select></td>";
        print "</tr>";
        print "<tr>";
        print "<td><input type='file' size=30 name='client_filename2' /></td> \n";
        print "<td><input type='text' size=30 name='server_filename2' /></td> \n";
        print "<td><select name='server_directory2' size='1'> \n";      
        print "<option value='upload'>        Upload  </option> \n";
        print "<option value='upload_image'>  Images   </option> \n";
        print "<option value='upload_other' > Other   </option> \n";
        print "</select></td>";
        print "</tr>";
        print "<tr>";
        print "<td><input type='submit' value='Upload File' /></td> \n";
        print "</tr>";
        print "</table>";       
        print "<font color=red>   $msg  </font><br/>";
        print "<font color=black> $msg2 </font><br/>";
        print "</form>";
}
?>
</div>

<?php include "../include.php"; ?>              <!-- hyperlink to see the code -->
</body>
</html>