Advertisement
desmonddevendran

Example PHP

Feb 12th, 2025
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.05 KB | Software | 0 0
  1. <?php
  2.  
  3. $uploadDir = __DIR__ . '/uploads/';
  4. if (!is_dir($uploadDir)) {
  5.     mkdir($uploadDir, 0755, true);
  6. }
  7.  
  8. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  9.     if (!isset($_FILES['file'])) {
  10.         die("No file uploaded.");
  11.     }
  12.    
  13.     $file = $_FILES['file'];
  14.     $fileTmp = $file['tmp_name'];
  15.     $fileName = basename($file['name']);
  16.    
  17.     $allowedMimeTypes = ['image/jpeg', 'image/png', 'image/gif'];
  18.    
  19.     if (!in_array($_SERVER['CONTENT_TYPE'], $allowedMimeTypes)) {
  20.         die("Invalid file type.");
  21.     }
  22.    
  23.     $destination = $uploadDir . $fileName;
  24.    
  25.     if (move_uploaded_file($fileTmp, $destination)) {
  26.         echo "File uploaded successfully: " . htmlspecialchars($fileName);
  27.     } else {
  28.         echo "Failed to upload file.";
  29.     }
  30. }
  31. ?>
  32.  
  33. <!DOCTYPE html>
  34. <html>
  35. <head>
  36.     <title>File Upload</title>
  37. </head>
  38. <body>
  39.     <form action="" method="POST" enctype="multipart/form-data">
  40.         <input type="file" name="file" required>
  41.         <button type="submit">Upload</button>
  42.     </form>
  43. </body>
  44. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement