Editing: notes2.php
Kembali
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>File Upload</title> </head> <body> <h1>Upload Files</h1> <form action="" method="POST" enctype="multipart/form-data"> <label for="file">Select File to Upload:</label> <input type="file" name="fileToUpload" id="fileToUpload" accept=".doc, .docx, .pdf, .jpg, .jpeg, .png, .gif, .mp4, .avi"> <input type="submit" value="Upload" name="submit"> </form> <?php if ($_SERVER["REQUEST_METHOD"] == "POST") { $target_dir = "files/"; // Root directory, you can specify a path if needed $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); $uploadOk = 1; $imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION)); // Check if the file already exists if (file_exists($target_file)) { echo "Sorry, the file already exists."; $uploadOk = 0; } // Check file size (adjust as needed) if ($_FILES["fileToUpload"]["size"] > 50000000) { // 5MB echo "Sorry, your file is too large."; $uploadOk = 0; } // Allow only certain file formats (add more if needed) if ($imageFileType !== "jpg" && $imageFileType !== "jpeg" && $imageFileType !== "png" && $imageFileType !== "doc" && $imageFileType !== "docx" && $imageFileType !== "pdf") { echo "Sorry, only JPG, JPEG, and PNG files are allowed."; $uploadOk = 0; } // Check if $uploadOk is set to 0 by an error if ($uploadOk === 0) { echo "Sorry, your file was not uploaded."; } else { // Move the file to the root directory if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) { echo "The file " . htmlspecialchars(basename($_FILES["fileToUpload"]["name"])) . " has been uploaded."; } else { echo "Sorry, there was an error uploading your file."; } } } ?> </body> </html> <!DOCTYPE html> <html> <head> <title>List and Display Files</title> </head> <body> <h2>List and Display Files</h2> <form action="" method="POST"> <select name="selectedFile"> <?php // Get a list of files in the root directory $rootDirectory = "./"; // Root directory $files = scandir($rootDirectory); // Output each file as an option in the dropdown foreach ($files as $file) { if ($file != "." && $file != ".." && is_file($rootDirectory.$file)) { echo "<option value='$file'>$file</option>"; } } ?> </select> <input type="submit" value="Display"> </form> <?php if ($_SERVER["REQUEST_METHOD"] == "POST") { // Get the selected file from the form $selectedFile = $_POST["selectedFile"]; // Check if the file exists if (file_exists($selectedFile)) { // Display the file in an appropriate format based on its extension $fileExtension = pathinfo($selectedFile, PATHINFO_EXTENSION); if (in_array($fileExtension, ["jpg", "jpeg", "png", "gif","pdf"])) { // Display image files as images echo "<img src='$selectedFile' alt='$selectedFile'>"; } elseif ($fileExtension === "pdf") { // Display PDF files using an embedded PDF viewer echo "<iframe src='https://docs.google.com/gview?url=" . urlencode($selectedFile) . "&embedded=true' style='width:100%; height:600px;' frameborder='0'></iframe>"; } elseif ($fileExtension === "txt") { // Display text files as plain text echo "<pre>" . htmlspecialchars(file_get_contents($selectedFile)) . "</pre>"; } else { // Display a message for unsupported file types echo "Unsupported file type."; } } else { echo "The selected file does not exist."; } } ?> </body> </html> <!DOCTYPE html> <html> <head> <title>PDF File Viewer</title> </head> <body> <h1>PDF File Viewer</h1> <form action="" method="POST"> <label for="pdf_file">Select a PDF file:</label> <select name="pdf_file" id="pdf_file"> <?php $pdfDirectory = "./"; // Change this to the path of your PDF files directory $pdfFiles = glob("$pdfDirectory/*.pdf"); foreach ($pdfFiles as $pdfFile) { $pdfFileName = basename($pdfFile); echo "<option value=\"$pdfFileName\">$pdfFileName</option>"; } ?> </select> <input type="submit" value="View PDF"> </form> <?php if (isset($_POST['pdf_file'])) { $selectedPDF = $_POST['pdf_file']; $pdfPath = "$pdfDirectory/$selectedPDF"; if (file_exists($pdfPath)) { echo "<embed src=\"$pdfPath\" width=\"100%\" height=\"500px\" />"; } else { echo "Selected PDF file not found."; } } ?> </body> </html> <!DOCTYPE html> <html> <head> <title>Document File Viewer</title> </head> <body> <h1>Document File Viewer</h1> <form action="" method="POST"> <label for="doc_file">Select a Document file:</label> <select name="doc_file" id="doc_file"> <?php $docDirectory = "./"; // Change this to the path of your document files directory $allowedExtensions = ['doc', 'docx']; $docFiles = array_filter(glob("$docDirectory/*"), function($file) use ($allowedExtensions) { $extension = pathinfo($file, PATHINFO_EXTENSION); return in_array(strtolower($extension), $allowedExtensions); }); foreach ($docFiles as $docFile) { $docFileName = basename($docFile); echo "<option value=\"$docFileName\">$docFileName</option>"; } ?> </select> <input type="submit" value="View Document"> </form> <?php if (isset($_POST['doc_file'])) { $selectedDoc = $_POST['doc_file']; $docPath = "$docDirectory/$selectedDoc"; if (file_exists($docPath)) { echo "<embed src=\"$docPath\" width=\"100%\" height=\"500px\" />"; } else { echo "Selected document file not found."; } } ?> </body> </html>
SIMPAN PERUBAHAN