Tuesday 9 September 2014

How to upload a file using PHP script



In this article let us discuss how to upload a file to a web server using PHP script. It is implemented as a function in this example in upload.php file. To upload a file, you have to create an HTML form with a file input control by using the input tag like this: "<input type=file>". 

While creating the HTML form, enctype attribute of the form should have the value "multipart/form-data". So the form tag will be like this:

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


HTML code to crate the form


<form action="upload.php" method="post" autocomplete="off" enctype="multipart/form-data">
        <table align="center" width="100%">
           
            <tr bgcolor="#ababee">
                <td colspan="2" align="center"><h2>Upload your file</h2></td>
            </tr>
            <tr>
                <td >Select file to upload</td>
                <td ><input type="file" name="file" id="file" style="left:0;">               
                </td>
            </tr>   
                          
           
            <tr>
                <td colspan="2">               
                <input type="submit" name="upload" value="Upload">
                </td>
            </tr>                
           
        </table>       
   </form>





PHP Script to upload the file

<?php   
        if (isset($_REQUEST['upload'])) {  
            $error = uploadimage();
            $error = $error=="" ? "File is uploaded successfully..." : $error;          
            echo "<h4  style='text-align:center;width:100%;background-color:yellow'>" . $error . "</h4>";                      
        }
   
        function uploadimage() {                 
           $ROOT_FOLDER ="images";
           $MAX_SIZE =  10485760 ; // 1024*1024 * 10 (10MB)
           $ALLOWED_EXTENSIONS=array("jpg", "jpeg", "gif", "png");
           $ALLOWED_FILE_TYPES=array("image/gif", "image/jpeg", "image/png", "image/pjpeg");

           $errorstring="";
           $exploded=explode(".", $_FILES["file"]["name"]);
           $extension = end($exploded);

            /* Upload error */  
            if ($_FILES["file"]["error"] > 0) {
                   $errorstring= $_FILES["file"]["error"];
                   return $errorstring;
            }

            /* Invalid file type */
            if (!in_array($_FILES["file"]["type"],$ALLOWED_FILE_TYPES)) {           
               $errorstring="Invalid file type";
               return $errorstring;
           }

   
        /* Large file > MAX_SIZE */
           if  ($_FILES["file"]["size"] > $MAX_SIZE ) {
               $errorstring="File is larger than permitted size";
               return $errorstring;
           }

            /* Invalid file type. Not in specified extensions */
           if (!in_array($extension, $ALLOWED_EXTENSIONS)) {
               $errorstring="Invalid file type/extension";
               return $errorstring;
           }

          
            /* File already exist */
           if (file_exists("./" . $ROOT_FOLDER .  "/" . $_FILES["file"]["name"])) {
                $errorstring=$_FILES["file"]["name"] . " already exists. ";
                return $errorstring;
           }

           /* Uploading ... */       
            move_uploaded_file($_FILES["file"]["tmp_name"],
                 "./" . $ROOT_FOLDER . "/" . $_FILES["file"]["name"]);                 


           return $errorstring;
       }
      

In this example both HTML form and PHP script  are implemented in a single file name ""upload.php". However these can be written in separate files one for HTML and another file for PHP script.


No comments:

Post a Comment