Created
December 31, 2025 17:49
-
-
Save Jerl92/800f179f3242a9c01aa4f174a8cdae42 to your computer and use it in GitHub Desktop.
Php upload file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <title>PHP File Upload</title> | |
| <link rel="stylesheet" href="index.css"> | |
| </head> | |
| <body> | |
| <div class="container"> | |
| <h1>PHP File Upload</h1> | |
| <form action="upload.php" method="POST" enctype="multipart/form-data"> | |
| <label for="file">Select a file:</label> | |
| <input type="file" name="uploaded_file" id="file" /> | |
| <button type="submit">Upload</button> | |
| </form> | |
| </div> | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| if ($_SERVER['REQUEST_METHOD'] === 'POST') { | |
| if (isset($_FILES['uploaded_file']) && $_FILES['uploaded_file']['error'] === UPLOAD_ERR_OK) { | |
| $fileTmpPath = $_FILES['uploaded_file']['tmp_name']; | |
| $fileName = $_FILES['uploaded_file']['name']; | |
| $uploadFolder = 'uploads/'; | |
| if (!is_dir(filename: $uploadFolder)) { | |
| mkdir(directory: $uploadFolder, permissions: 0755, recursive: true); | |
| } | |
| $destination = $uploadFolder . basename(path: $fileName); | |
| if (move_uploaded_file(from: $fileTmpPath, to: $destination)) { | |
| echo "File uploaded: " . htmlspecialchars(string: $fileName); | |
| } else { | |
| echo "Error moving the uploaded file."; | |
| } | |
| } else { | |
| echo "An error occurred."; | |
| } | |
| } | |
| ?> |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://cloudinary.com/guides/front-end-development/upload-file-in-php