📁 Folder Navigator
Position:
Root
/
apphome
/
faith
/
vpnp1
/
admin
/
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:
read2.txt
To create a single PHP file that handles form submission (including file uploads), data storage, and display in an HTML table, you will use a self-processing form with PHP logic embedded. The file will check if the form has been submitted and, if so, process the data, move the uploaded file to a server directory, and append the information to a data file (e.g., a CSV or a serialized file). It will then read all stored data and display it in an HTML table, including an <img> tag for the uploaded image. brand_form.php php <?php // Prevent timezone warnings date_default_timezone_set('America/New_York'); // Define upload directory and data file $uploadDir = "uploads/"; $dataFile = "data.csv"; // Ensure the upload directory exists if (!is_dir($uploadDir)) { mkdir($uploadDir, 0755, true); } // Function to sanitize input data function sanitize_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } // Handle form submission if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['submit'])) { $brand_intro = sanitize_input($_POST['brand_intro']); $contact_info = sanitize_input($_POST['contact_info']); $fileName = null; $statusMsg = ""; // Handle file upload if (isset($_FILES["brand_image"]) && $_FILES["brand_image"]["error"] == 0) { $image = basename($_FILES["brand_image"]["name"]); $targetFilePath = $uploadDir . $image; $fileType = strtolower(pathinfo($targetFilePath, PATHINFO_EXTENSION)); // Allow certain file formats $allowTypes = array('jpg', 'png', 'jpeg', 'gif'); if (in_array($fileType, $allowTypes)) { // Move file to server if (move_uploaded_file($_FILES["brand_image"]["tmp_name"], $targetFilePath)) { $fileName = $image; $statusMsg = "The file " . htmlspecialchars($image) . " has been uploaded."; } else { $statusMsg = "Sorry, there was an error uploading your file."; } } else { $statusMsg = "Sorry, only JPG, JPEG, PNG, GIF files are allowed to upload."; } } else { $statusMsg = "No file uploaded or an error occurred."; } // Store data if upload was successful if ($fileName) { $data = array($brand_intro, $contact_info, $fileName); // Append to CSV file $fileHandle = fopen($dataFile, 'a'); fputcsv($fileHandle, $data); fclose($fileHandle); } echo "<p>$statusMsg</p>"; } // Read all stored data for display $storedData = []; if (file_exists($dataFile)) { $fileHandle = fopen($dataFile, 'r'); while (($row = fgetcsv($fileHandle)) !== FALSE) { $storedData[] = $row; } fclose($fileHandle); } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Brand Information Form</title> <style> table { border-collapse: collapse; width: 100%; } th, td { border: 1px solid #ddd; padding: 8px; text-align: left; } th { background-color: #f2f2f2; } img { max-width: 100px; height: auto; } form { margin-bottom: 20px; } </style> </head> <body> <h2>Submit Brand Information</h2> <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post" enctype="multipart/form-data"> <label for="brand_intro">Brand Introduction:</label><br> <textarea id="brand_intro" name="brand_intro" rows="4" cols="50" required></textarea><br><br> <label for="contact_info">Contact Info (Email/Phone):</label><br> <input type="text" id="contact_info" name="contact_info" required><br><br> <label for="brand_image">Brand Image:</label><br> <input type="file" id="brand_image" name="brand_image" accept="image/png, image/jpeg, image/gif"><br><br> <input type="submit" name="submit" value="Submit Information"> </form> <h2>Stored Brand Information</h2> <table> <thead> <tr> <th>Brand Introduction</th> <th>Contact Info</th> <th>Image</th> </tr> </thead> <tbody> <?php foreach ($storedData as $row): ?> <tr> <td><?php echo htmlspecialchars($row[0]); ?></td> <td><?php echo htmlspecialchars($row[1]); ?></td> <td> <?php if (isset($row[2]) && $row[2]): ?> <img src="<?php echo htmlspecialchars($uploadDir . $row[2]); ?>" alt="Brand Image"> <?php else: ?> No Image <?php endif; ?> </td> </tr> <?php endforeach; ?> </tbody> </table> </body> </html> Use code with caution. Key Components Single File (brand_form.php): The action attribute of the form is set to htmlspecialchars($_SERVER["PHP_SELF"]), which sends the data back to the same PHP file for processing. enctype="multipart/form-data": This is essential in the <form> tag for file uploads to work correctly with the POST method. $_FILES Array: PHP uses the $_FILES superglobal array to handle uploaded files. move_uploaded_file(): This PHP function securely moves the file from the temporary directory to the permanent uploads/ folder on the server. Data Storage: The script uses fputcsv() to append the text data and the image filename to a data.csv file. Display: The script reads the data.csv file using fgetcsv() and iterates through the data to populate an HTML table dynamically. The <img> tag's src attribute is set to the path of the uploaded image. Security: htmlspecialchars() is used to prevent XSS (Cross-Site Scripting) attacks when displaying user input. Simple file type validation is also included. Setup Instructions Save the code: Save the entire code block above as a file named brand_form.php. Create an upload directory: In the same directory where you saved brand_form.php, create a folder named uploads. Ensure permissions: Make sure your web server has write permissions for the uploads folder and the data.csv file (once created by the script). Run: Access the file in your web browser via your local server (e.g., http://localhost/brand_form.php). How can I secure file uploads more effectively in this PH
💾 Save Changes