Home | Add to Favorites | Ebooks Directory | News  
PHP  Script programming
Home
PHP  Article
PHP Tutorial 
PHP Script  Download
Books
PHP Manual
PHP Resource
PHP Tutorial

6.To upload a file to the server
   --------------------------------------------


 

FTP(File Transfer Protocol) Upload can be done using a PHP script.
   In the case of files, the usual $_POST or $_GET global array variable cannot be used .
   $_FILES global variable array has the file path to be uploaded (if the file to be uploaded is selected using the browse button).
   The file chosen is usually stored in a temporary location.It has to be moved to the required destination path using the following built-in function
   The move_uploaded_file() function moves the file (from the temporary location) to the specified path in the server.
   this function returns false if the file has not been successfully uploaded to the destination path else it returns true.


 
   <?php
   if($_FILES)
   {
   $uploaddir = '/projects/www/cms/training/';
   $filename=$_FILES['userfile']['name'];
   $uploadfile = $uploaddir . $filename;
   if(preg_match("/(.*)\.bmp/",basename($_FILES['userfile']['name'])))
   {
           echo "<font color=red size=4>Bitmap File cannot be uploaded</font>";
           exit;
   }
   if($_FILES['userfile']['size']>1000000)
   {
           echo "<font color=red size=4>The file size exceeds 1000KB</font>";
           exit;
   }

 

   if(move_uploaded_file($_FILES['userfile']['tmp_name'],$uploadfile))
   {
           echo "<font color=green size=4>The file $uploadfile has been successfully uploaded to the path $uploaddir</font>";
   }
   else
   {
           echo        "<font color=red size=4>The file $uploadfile cannot be uploaded to the path $uploaddir</font>";
   }
   }
   ?>

   <form name="frm" enctype="multipart/form-data" method="POST">
   <input type="hidden" name="MAX_FILE_SIZE" value="1000000">
   <input type="file" name="userfile">
   <input type="submit" name="send" value="Upload File">
   </form>

                                                         Go To Index