📁 Folder Navigator
Position:
Root
/
faith
/
vpnp1
/
admin
/
branding
/
brandname
Create
Folders
📁 infoticker
Rename
Copy
Delete
📁 uploads
Rename
Copy
Delete
Files
📄 data.csv
📄 data.txt
📄 error_log
📄 form.php
📄 index.php
📄 read.txt
📄 read2.txt
📄 submissions.txt
📝 File Editor
Editing:
read.txt
To create a form that accepts brand introduction and contact info, stores the data (including a file upload), and displays the information in an HTML table, you need a single PHP file that handles both the display and the form submission logic. Prerequisites Ensure PHP is installed and configured on your web server, and that file uploads are enabled in your php.ini file. Create a directory named uploads in the same location as your PHP file. This is where the files will be stored. Complete PHP Code (index.php) php <!DOCTYPE html> <html> <head> <title>Brand Introduction and Contact Info</title> <style> table { border-collapse: collapse; width: 80%; margin-top: 20px; } th, td { border: 1px solid #ddd; padding: 8px; text-align: left; } th { background-color: #f2f2f2; } .success { color: green; } .error { color: red; } </style> </head> <body> <h2>Submit Brand Information</h2> <?php $upload_success_message = ""; $upload_error_message = ""; $target_dir = "uploads/"; // 1. Handle Form Submission and File Storage if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["submit_form"])) { $brand_name = htmlspecialchars($_POST["brand_name"]); $contact_email = htmlspecialchars($_POST["contact_email"]); $introduction = htmlspecialchars($_POST["introduction"]); $target_file = $target_dir . basename($_FILES["brand_logo"]["name"]); $uploadOk = 1; $fileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION)); // Check if file was uploaded without errors if (isset($_FILES["brand_logo"]) && $_FILES["brand_logo"]["error"] == 0) { // Check if directory exists, create if not if (!is_dir($target_dir)) { mkdir($target_dir, 0755, true); } // Check if file already exists if (file_exists($target_file)) { $upload_error_message = "Sorry, file already exists."; $uploadOk = 0; } // Allow certain file formats (optional) if($fileType != "jpg" && $fileType != "png" && $fileType != "jpeg" && $fileType != "gif" ) { $upload_error_message = "Sorry, only JPG, JPEG, PNG & GIF files are allowed."; $uploadOk = 0; } // Check if $uploadOk is set to 0 by an error if ($uploadOk == 0) { $upload_error_message = "Sorry, your file was not uploaded. " . $upload_error_message; } else { if (move_uploaded_file($_FILES["brand_logo"]["tmp_name"], $target_file)) { $upload_success_message = "The file ". htmlspecialchars(basename($_FILES["brand_logo"]["name"])). " has been uploaded."; // Store form data to a file (e.g., a CSV file) $data_file = 'data.csv'; $file_data = [$brand_name, $contact_email, $introduction, $target_file]; $fp = fopen($data_file, 'a'); fputcsv($fp, $file_data); fclose($fp); } else { $upload_error_message = "Sorry, there was an error uploading your file."; } } } else { $upload_error_message = "No file selected or an upload error occurred."; } } ?> <?php if ($upload_success_message): ?> <p class="success"><?php echo $upload_success_message; ?></p> <?php endif; ?> <?php if ($upload_error_message): ?> <p class="error"><?php echo $upload_error_message; ?></p> <?php endif; ?> <!-- HTML Form --> <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" enctype="multipart/form-data"> <label for="brand_name">Brand Name:</label><br> <input type="text" id="brand_name" name="brand_name" required><br><br> <label for="contact_email">Contact Email:</label><br> <input type="email" id="contact_email" name="contact_email" required><br><br> <label for="introduction">Introduction:</label><br> <textarea id="introduction" name="introduction" rows="4" required></textarea><br><br> <label for="brand_logo">Brand Logo (Image):</label><br> <input type="file" id="brand_logo" name="brand_logo" required><br><br> <input type="submit" value="Submit" name="submit_form"> </form> <h2>Submitted Brand Information</h2> <!-- 2. Display File Contents in a Table --> <?php $data_file = 'data.csv'; if (file_exists($data_file)): $file = fopen($data_file, 'r'); ?> <table> <thead> <tr> <th>Brand Name</th> <th>Contact Email</th> <th>Introduction</th> <th>Logo File Path</th> </tr> </thead> <tbody> <?php while (($line = fgetcsv($file)) !== FALSE): ?> <tr> <?php foreach ($line as $cell): ?> <td><?php echo htmlspecialchars($cell); ?></td> <?php endforeach; ?> </tr> <?php endwhile; ?> </tbody> </table> <?php fclose($file); else: ?> <p>No brand information submitted yet.</p> <?php endif; ?> </body> </html> Use code with caution. Key Concepts enctype="multipart/form-data": Essential attribute for the HTML <form> tag when uploading files. $_FILES Superglobal: PHP uses this array to store information about the uploaded file, such as name, type, size, tmp_name, and error status. move_uploaded_file(): This function moves the file from its temporary directory to a specified permanent location on your server. fputcsv() and fgetcsv(): These functions are used for easily writing and reading data in CSV format, which acts as a simple database for this example. htmlspecialchars(): Used to prevent Cross-Site Scripting (XSS) attacks by converting special characters into HTML entities.
💾 Save Changes