Tuesday, July 16, 2013

This is the simple way to upload the files to server via PHP


Here' s the HTML


<html>
<body>

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

  <label for="file">Filename:</label>
 <input type="file" name="ufile" id="file">
 <input type="submit" name="submit" value="Submit">

  </form> 
 </body> 
 </html>  


Here is the php script



<?php
$SafeFile = $_FILES['ufile']['name'];

$uploaddir ="C:/";                                  
$path = $uploaddir.$SafeFile;                        //DESTINATION PATH

if($SafeFile != " ") {
    if(copy($_FILES['ufile']['tmp_name'], $path)) {
        $theFileName = $_FILES['ufile']['name'];
        $theFileSize = $_FILES['ufile']['size'];
        if ($theFileSize>999999) {
            $theDiv = $theFileSize / 1000000;
            $theFileSize = round($theDiv, 1)." MB";
        } else { //OTHERWISE DISPLAY AS KB
            $theDiv = $theFileSize / 1000;
            $theFileSize = round($theDiv, 1)." KB";
        }


// DISPLAY CORRESPONDING MESSAGES

//UPLOAD SUCCESSFUL



        echo <<<upload_sucess
        <table cellpadding="5" width="300">

       <tr>
      <td align="Center" colspan="2">
      <font color="#009900">
      <b>Upload Successful</b>
      </font>
       </td>
      </tr>
      <tr>
      <td align="right"><b>File Name: </b></td>
      <td align="left">$theFileName</td>
       </tr>
       <tr>
        <td align="right"><b>File Size: </b></td>
        <td align="left">$theFileSize</td>
      </tr>
      <tr>
    <td align="right"><b>Directory: </b></td>
    <td align="left">$uploaddir</td>
    </tr>
    </table>

upload_sucess;

    }


 //FAILURE

    else {
        echo <<<upload_failure
               <table cellpadding="5" width="80%">
                   <tr>
                       <td align="Center" colspan="2"><font color=\"#C80000\"><b>File could not be uploaded</b></font></td>
                   </tr>
                </table>
upload_failure;
    }
}

?>


DOWNLOAD THE HTML CODE

 

DOWNLOAD PHP CODE



For more info visit
www.w3schools.com/php/php_file_upload.asp

No comments:

Post a Comment