Certainly! Below is a simple PHP script for a one-page file manager. This script allows users to upload files, view the list of uploaded files, and delete files. It is a basic implementation, and you may want to enhance it for security, user authentication, and other features before using it in a production environment.
Here's the code:
<?php
$uploadDir = 'uploads/';
$allowedTypes = ['image/jpeg', 'image/png', 'image/gif', 'application/pdf', 'text/plain'];
if (!is_dir($uploadDir)) {
mkdir($uploadDir, 0755, true);
}
// Handle file upload
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['fileToUpload'])) {
$file = $_FILES['fileToUpload'];
if (in_array($file['type'], $allowedTypes) && $file['error'] === UPLOAD_ERR_OK) {
$destinationPath = $uploadDir . basename($file['name']);
if (move_uploaded_file($file['tmp_name'], $destinationPath)) {
echo "<p>File uploaded successfully: {$file['name']}</p>";
} else {
echo "<p>Error uploading file.</p>";
}
} else {
echo "<p>Invalid file type or error in file upload.</p>";
}
}
// Handle file deletion
if (isset($_POST['delete'])) {
$fileToDelete = $uploadDir . basename($_POST['delete']);
if (file_exists($fileToDelete)) {
unlink($fileToDelete);
echo "<p>File deleted successfully: {$_POST['delete']}</p>";
} else {
echo "<p>File not found.</p>";
}
}
$files = array_diff(scandir($uploadDir), array('..', '.'));
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>PHP File Manager</title>
</head>
<body>
<h1>Simple PHP File Manager</h1>
<h2>Upload a File</h2>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="fileToUpload" required>
<input type="submit" value="Upload File">
</form>
<h2>Uploaded Files</h2>
<ul>
<?php foreach ($files as $file): ?>
<li>
<?php echo htmlspecialchars($file); ?>
<form action="" method="post" style="display: inline;">
<input type="hidden" name="delete" value="<?php echo htmlspecialchars($file); ?>">
<input type="submit" value="Delete" onclick="return confirm('Are you sure you want to delete this file?');">
</form>
</li>
<?php endforeach; ?>
</ul>
</body>
</html>
Explanation:
-
Directory Setup: The script creates an uploads/ directory if it doesn't exist. This is where uploaded files will be stored.
-
File Upload Handling: When a file is uploaded, the script checks its type and moves it to the uploads/ directory if valid.
-
File Deletion Handling: Users can delete files using a simple form. If the file exists, it will be deleted.
-
File Listing: The script lists all the files in the uploads/ directory and provides a delete option for each file.
Usage Instructions:
- Create a file named
file_manager.php (or similar) and paste the code into it.
- Make sure your web server can write to the directory where the script is located.
- Access the script through your web browser to test the file manager.
Important Considerations:
- Security: This is a basic implementation. Make sure to validate and sanitize user inputs to prevent security vulnerabilities such as directory traversal attacks or file uploads of dangerous file types.
- File Permissions: Set appropriate permissions on the
uploads/ directory to avoid unauthorized access.
- File Size Limit: You may want to set a limit on the size of the files that can be uploaded.
- User Authentication: Consider implementing user authentication if you plan to use this in a multi-user environment.