Page 264 - Computer Software Application TP - Volume 1
P. 264

COMPUTER SOFTWARE APPLICATION - CITS




                     <body>
                     <form action=”upload.php” method=”post” enctype=”multipart/form-data”>
                         <label for=”file”>Select a file:</label>
                         <input type=”file” name=”file” id=”file” required>
                         <br>
                         <input type=”submit” value=”Upload File”>
                     </form>
                     </body>
                     </html>

                 •  Save the program in C:\Apache24\htdocs in a folder as index.html
              2  Create PHP Script to Handle Upload
                 •  Open the text editor
                 •  Write the following codes
                            <html>
                              <body>
                        <?php
                               // upload.php


                              if ($_SERVER[“REQUEST_METHOD”] == “POST”)
                              {
                                      // Check if the file was uploaded without errors
                                      if (isset($_FILES[“file”]) && $_FILES[“file”][“error”] == 0)
                                      {
                                               $allowedTypes = [‘jpg’, ‘jpeg’, ‘png’, ‘gif’, ‘txt’];
                                               $maxSize = 5 * 1024 * 1024; // 5 MB

                                               $targetDir = “uploads/”; //create a folder in this name in your php file saved location .
                                               $targetFile = $targetDir . basename($_FILES[“file”][“name”]);
                                               $fileExtension = strtolower(pathinfo($targetFile,          PATHINFO_EXTENSION));


                                               // Check file type
                                               if (!in_array($fileExtension, $allowedTypes))
                                               {
                                                     echo “Error: Invalid file type.”;
                                               }
                                               elseif ($_FILES[“file”][“size”] > $maxSize)
                                               {
                                                      echo “Error: File size exceeds the limit.”;
                                               }
                                               else
                                               {
                                                       // Move the uploaded file to the target directory
                                                       if (move_uploaded_file($_FILES[“file”][“tmp_name”], $targetFile))
                                                      {





                                                           249

                               CITS : IT & ITES - Computer Software Application - Exercise 53
   259   260   261   262   263   264   265   266   267   268   269