Page 275 - CITS - CSA - TP (Volume 1) - Hindi
P. 275

कंप्यूटर सॉफ्टवेयर एप्लीकेशन- CITS




                        <?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))

                                                      {
                                                            echo “The file has been uploaded successfully.”;
                                                      }
                                                      else
                                                      {
                                                            echo “Sorry, there was an error uploading your file.”;

                                                      }
                                               }
                                     }






                                                           259

                                     CITS : IT & ITES - कं  ूटर सॉ वेयर ए ीके शन - अ ास 53
   270   271   272   273   274   275   276   277   278   279   280