Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $uploadDir = __DIR__ . '/uploads/';
- if (!is_dir($uploadDir)) {
- mkdir($uploadDir, 0755, true);
- }
- if ($_SERVER['REQUEST_METHOD'] === 'POST') {
- if (!isset($_FILES['file'])) {
- die("No file uploaded.");
- }
- $file = $_FILES['file'];
- $fileTmp = $file['tmp_name'];
- $fileName = basename($file['name']);
- $allowedMimeTypes = ['image/jpeg', 'image/png', 'image/gif'];
- if (!in_array($_SERVER['CONTENT_TYPE'], $allowedMimeTypes)) {
- die("Invalid file type.");
- }
- $destination = $uploadDir . $fileName;
- if (move_uploaded_file($fileTmp, $destination)) {
- echo "File uploaded successfully: " . htmlspecialchars($fileName);
- } else {
- echo "Failed to upload file.";
- }
- }
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <title>File Upload</title>
- </head>
- <body>
- <form action="" method="POST" enctype="multipart/form-data">
- <input type="file" name="file" required>
- <button type="submit">Upload</button>
- </form>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement