Skip to content

Instantly share code, notes, and snippets.

@Jerl92
Created December 31, 2025 17:49
Show Gist options
  • Select an option

  • Save Jerl92/800f179f3242a9c01aa4f174a8cdae42 to your computer and use it in GitHub Desktop.

Select an option

Save Jerl92/800f179f3242a9c01aa4f174a8cdae42 to your computer and use it in GitHub Desktop.
Php upload file
<!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>
<?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.";
}
}
?>
@Jerl92
Copy link
Author

Jerl92 commented Dec 31, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment