Aman PHP Assignment 3
Aman PHP Assignment 3
1). Create a PHP web application where users can upload files, view the list of
uploaded files, download them, and receive an email confirmation after file
uploads. Use form redirection and date handling to display the time the file
was uploaded.
Ans- Index.php
< html>
<head>
<title>File Upload</title>
</head>
<body>
<h2>Upload a File</h2>
<form action="upload.php" method="POST" enctype="multipart/form-data">
<input type="file" name="fileToUpload" required>
<input type="submit" name="submit" value="Upload File">
</form> <br>
<a href="list_files.php">View Uploaded Files</a>
</body>
</html>
Upload.php
<?php
if (isset($_POST['submit'])) {
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$fileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
// Check if upload is ok
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
// Save the current timestamp
$upload_time = date("Y-m-d H:i:s");
$files = scandir($dir);
download.php
<?php
if (isset($_GET['file'])) {
$file = urldecode($_GET['file']); // Decode the URL-encoded file name
$filepath = "uploads/" . $file;
if (file_exists($filepath)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . basename($filepath));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filepath));
readfile($filepath);
exit;
} else {
echo "File does not exist.";
}
} else {
echo "No file specified.";
}
?>
Output
File uploaded successfully at 2024-10-07 14:32:12
Subject: File Uploaded Successfully
Your file example.txt has been successfully uploaded on 2024-10-07 14:32:12.
2) Develop a PHP application where users can submit feedback or complaints, upload
relevant documents, and receive a PDF report summarizing their submission. Send an
email confirmation with the report attached.
Ans-
index.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Submit Feedback/Complaint</title>
</head>
<body>
<h2>Submit Feedback or Complaint</h2>
<form action="submit.php" method="POST" enctype="multipart/form-data">
<label for="name">Name:</label>
<input type="text" name="name" required><br>
<label for="email">Email:</label>
<input type="email" name="email" required><br>
submit.php
<?php
require('TCPDF/tcpdf.php');
require('PHPMailer/PHPMailerAutoload.php');
require('config.php'); // Email configuration
if (isset($_POST['submit'])) {
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$message = htmlspecialchars($_POST['message']);
$uploadOk = 1;
$filePath = '';
if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
$filePath = $target_file;
} else {
echo "There was an error uploading the file.";
$uploadOk = 0;
}
}
if ($filePath) {
$pdf->Cell(0, 10, "Attached Document: " . basename($filePath), 0, 1);
}
if ($filePath) {
$mail->addAttachment($filePath); // Attach uploaded document
}
if ($mail->send()) {
echo "Feedback submitted successfully! A confirmation email has been sent.";
} else {
echo "Error in sending email: " . $mail->ErrorInfo;
}
}
?>
config.php
<?php
require 'PHPMailer/PHPMailerAutoload.php';
3) Create a simple PHP application where users can upload articles, view a list of articles,
download content as a PDF, and receive email notifications whenever a new article is
posted.
Ans-
Index.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Submit Article</title>
</head>
<body>
<h2>Submit a New Article</h2>
<form action="submit_article.php" method="POST">
<label for="title">Article Title:</label>
<input type="text" name="title" required><br><br>
<br>
<a href="list_articles.php">View All Articles</a>
</body>
</html>
submit_article.php
<?php
require('TCPDF/tcpdf.php');
require('PHPMailer/PHPMailerAutoload.php');
require('config.php'); // Email configuration
if (isset($_POST['submit'])) {
$title = htmlspecialchars($_POST['title']);
$content = htmlspecialchars($_POST['content']);
$authorEmail = htmlspecialchars($_POST['author']);
if ($mail->send()) {
echo "Article submitted successfully! A notification email has been sent.";
} else {
echo "Error in sending email: " . $mail->ErrorInfo;
}
list_articles.php
<?php
$dir = "articles/";
$files = scandir($dir);
if (count($files) > 2) {
echo "<ul>";
foreach ($files as $file) {
if ($file != "." && $file != "..") {
// Extract the article title from the file content
$filePath = $dir . $file;
$fileContent = file_get_contents($filePath);
$lines = explode("\n", $fileContent);
$title = str_replace("Title: ", "", $lines[0]);
if (file_exists($pdfFile)) {
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="' . basename($pdfFile) . '"');
readfile($pdfFile);
exit;
} else {
echo "PDF file not found.";
}
} else {
echo "No article specified.";
}
?>
config.php
<?php
require 'PHPMailer/PHPMailerAutoload.php';
$mail->isSMTP();
$mail->Host = 'smtp.example.com'; // Replace with your SMTP host
$mail->SMTPAuth = true;
$mail->Username = '[email protected]'; // Replace with your email
$mail->Password = 'your-email-password'; // Replace with your email password
$mail->SMTPSecure = 'tls';
$mail->Port = 587;