Here is a PHP script that creates a website with a beautiful design using HTML, PHP, and Tailwind CSS v3. The website includes a rainbow panel with three tabs:
- Upload Photo: Allows users to upload photos with extensions jpg, png, or webp.
- View Photos: Displays all uploaded photos on the site.
- Current Date & Time: Shows the current date and time.
Note: Make sure to have PHP installed and configured on your server. Also, create an uploads directory with write permissions, or the script will create it automatically.
index.php
<?php
// Handle file upload
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_FILES['image'])) {
$allowedExtensions = ['jpg', 'jpeg', 'png', 'webp'];
$uploadDir = 'uploads/';
// Create the uploads directory if it doesn't exist
if (!is_dir($uploadDir)) {
mkdir($uploadDir, 0755, true);
}
$fileName = basename($_FILES['image']['name']);
$targetFilePath = $uploadDir . $fileName;
$fileType = pathinfo($targetFilePath, PATHINFO_EXTENSION);
// Check if the file has an allowed extension
if (in_array(strtolower($fileType), $allowedExtensions)) {
// Move the uploaded file to the uploads directory
if (move_uploaded_file($_FILES['image']['tmp_name'], $targetFilePath)) {
$uploadSuccess = "File uploaded successfully.";
} else {
$uploadError = "Error uploading the file.";
}
} else {
$uploadError = "Invalid file type. Only JPG, PNG, and WEBP files are allowed.";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Rainbow Panel Website</title>
<!-- Tailwind CSS CDN -->
<script src="https://cdn.tailwindcss.com"></script>
<style>
/* Custom styles for rainbow gradient */
.rainbow-panel {
background: linear-gradient(to right, red, orange, yellow, green, blue, indigo, violet);
}
</style>
</head>
<body class="min-h-screen flex items-center justify-center bg-gray-100">
<div class="w-full max-w-4xl p-6 rounded-lg shadow-lg rainbow-panel">
<!-- Tabs -->
<div class="bg-white rounded-lg">
<ul class="flex border-b" id="tabs">
<li class="-mb-px mr-1">
<a href="#tab1" class="bg-white inline-block py-2 px-4 text-blue-500 font-semibold">Upload Photo</a>
</li>
<li class="mr-1">
<a href="#tab2" class="bg-white inline-block py-2 px-4 text-blue-500 hover:text-blue-800 font-semibold">View Photos</a>
</li>
<li class="mr-1">
<a href="#tab3" class="bg-white inline-block py-2 px-4 text-blue-500 hover:text-blue-800 font-semibold">Current Date & Time</a>
</li>
</ul>
<!-- Tab Contents -->
<div id="tab-contents">
<!-- Tab 1: Upload Photo -->
<div id="tab1">
<div class="p-4">
<h2 class="text-xl font-semibold mb-4">Upload Photo</h2>
<?php if (isset($uploadSuccess)) : ?>
<p class="text-green-500 mb-4"><?php echo $uploadSuccess; ?></p>
<?php elseif (isset($uploadError)) : ?>
<p class="text-red-500 mb-4"><?php echo $uploadError; ?></p>
<?php endif; ?>
<form action="" method="post" enctype="multipart/form-data">
<div class="mb-4">
<input type="file" name="image" accept=".jpg,.jpeg,.png,.webp" class="block w-full text-sm text-gray-700 border border-gray-300 rounded-lg cursor-pointer focus:outline-none">
</div>
<button type="submit" class="bg-blue-500 text-white px-4 py-2 rounded">Upload</button>
</form>
</div>
</div>
<!-- Tab 2: View Photos -->
<div id="tab2" class="hidden">
<div class="p-4">
<h2 class="text-xl font-semibold mb-4">Uploaded Photos</h2>
<div class="grid grid-cols-1 md:grid-cols-3 gap-4">
<?php
$uploadDir = 'uploads/';
$imageFiles = glob($uploadDir . "*.{jpg,jpeg,png,webp}", GLOB_BRACE);
if (count($imageFiles) > 0) {
foreach ($imageFiles as $file) {
echo '<div class="border rounded-lg overflow-hidden">';
echo '<img src="' . $file . '" alt="Uploaded Image" class="w-full h-48 object-cover">';
echo '</div>';
}
} else {
echo '<p>No images uploaded.</p>';
}
?>
</div>
</div>
</div>
<!-- Tab 3: Current Date & Time -->
<div id="tab3" class="hidden">
<div class="p-4">
<h2 class="text-xl font-semibold mb-4">Current Date & Time</h2>
<p class="text-lg">
<?php
date_default_timezone_set('UTC'); // Set your desired timezone
echo date('l, F j, Y, g:i A');
?>
</p>
</div>
</div>
</div>
</div>
</div>
<!-- JavaScript to switch tabs -->
<script>
const tabs = document.querySelectorAll('#tabs a');
const tabContents = document.querySelectorAll('#tab-contents > div');
// Function to switch tabs
tabs.forEach((tab, index) => {
tab.addEventListener('click', (e) => {
e.preventDefault();
// Remove active classes
tabs.forEach(t => t.classList.remove('border-b-2', 'border-blue-500', 'text-blue-500'));
tabContents.forEach(c => c.classList.add('hidden'));
// Add active classes to the clicked tab
tabs[index].classList.add('border-b-2', 'border-blue-500', 'text-blue-500');
tabContents[index].classList.remove('hidden');
});
});
</script>
</body>
</html>
Explanation:
- Rainbow Panel: The
.rainbow-panel class applies a linear gradient to create the rainbow effect.
- Tabs Navigation: Implemented using an unordered list
<ul> with anchor tags for each tab.
- Tab Contents: Each tab content is a
<div> with a corresponding ID (tab1, tab2, tab3).
Uploading Photos (Tab 1):
- Form Handling: The PHP script at the top checks if a file has been submitted and handles the upload.
- Allowed Extensions: Only files with extensions
jpg, jpeg, png, or webp are accepted.
- Upload Directory: Uploaded images are stored in the
uploads directory.
Viewing Photos (Tab 2):
- Displaying Images: Uses
glob() to fetch all image files from the uploads directory and displays them in a grid layout.
Current Date & Time (Tab 3):
- Displaying Date & Time: Uses PHP's
date() function to display the current date and time. Adjust the timezone as needed.
Tab Switching JavaScript:
- Event Listeners: Adds click event listeners to the tab links to switch between tabs without reloading the page.
- Active Tab Styling: Updates the classes to highlight the active tab.
Set Up Instructions:
- Create a Directory: Place the
index.php file in your web server's root directory or a subdirectory.
- Create Uploads Folder: Ensure there's an
uploads folder with appropriate write permissions. If it doesn't exist, the script will attempt to create it.
- Adjust Permissions: Make sure the web server has permission to write to the
uploads directory.
- Run the Script: Access the
index.php file via your web browser.
Screenshot of the Website:
Since I can't provide actual images, when you run the script, you will see a colorful panel with three tabs as described.
Feel free to customize the design and functionality further to suit your needs. Let me know if you have any questions or need additional assistance!