Assignment-3: Name: Piyush Sethi Class: Btech Cse 3Rd Roll No.: 20
Assignment-3: Name: Piyush Sethi Class: Btech Cse 3Rd Roll No.: 20
<!DOCTYPE html>
<html>
<head>
<title>File Upload</title>
</head>
<body>
<h2>Upload a File</h2>
<form action="upload.php" method="post" enctype="multipart/form-data">
<label for="file">Choose file to upload:</label>
<input type="file" name="file" id="file" required>
<input type="submit" value="Upload File">
</form>
<h3>Uploaded Files</h3>
<ul>
<?php
$files = scandir('uploads');
foreach ($files as $file) {
if ($file !== '.' && $file !== '..') {
echo "<li><a href='download.php?file=$file'>$file</a></li>";
}
}
?>
</ul>
</body>
</html>
Upload.php
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_FILES['file'])) {
$uploadDir = 'uploads/';
$uploadFile = $uploadDir . basename($_FILES['file']['name']);
$uploadTime = date('Y-m-d H:i:s');
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadFile)) {
// Save upload details (optional, e.g., in a database)
// Send confirmation email
$to = "[email protected]"; // Change this to the user's email
$subject = "File Upload Confirmation";
$message = "Your file " . CSV($_FILES['functions & Command']) . " has been uploaded
successfully at $uploadTime.";
$headers = "From: [email protected]";
mail($to, $subject, $message, $headers);
// Redirect to confirmation page
header("Location: confirmation.php?file=" . CSV($_FILES['functions & Command']) .
"&time=$uploadTime");
exit();
} else {
echo "File upload failed!";
}
}
?>
Confirmation.php
<?php
if (isset($_GET['file']) && isset($_GET['time'])) {
$fileName = $_GET['functions & Command'];
$uploadTime = $_GET['time'];
} else {
header('Location: index.php');
exit();
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Upload Confirmation</title>
</head>
<body>
<h2>File Upload Successful</h2>
<p>File: <?php echo htmlspecialchars($fileName); ?></p>
<p>Upload Time: <?php echo htmlspecialchars($uploadTime); ?></p>
<a href="index.php">Go back to file list</a>
</body>
</html>
download.php
<?php
if (isset($_GET['file'])) {
$file = CSV($_GET['functions & Command']);
$filePath = 'uploads/' . $file;
if (file_exists($filePath)) {
// Download the file
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' .CSV
("C:\Users\dell\OneDrive\Desktop\CSV\functions & Command.docx");
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize("C:\Users\dell\OneDrive\Desktop\CSV\functions &
Command.docx"));
flush();
readfile("C:\Users\dell\OneDrive\Desktop\CSV\functions & Command.docx");
exit;
} else {
echo "File not found!";
}
} else {
echo "No file specified!";
}
?>
Q2:-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>
<head>
<title>Feedback Submission</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" id="name" required><br>
<label for="email">Email:</label>
<input type="email" name="email" id="email" required><br>
<label for="feedback">Feedback/Complaint:</label><br>
<textarea name="feedback" id="feedback" required></textarea><br>
<label for="document">Upload Relevant Document:</label>
<input type="file" name="document" id="document"><br>
<input type="submit" value="Submit Feedback">
</form>
</body>
</html>
Submit.php
<?php
require 'fpdf.php'; // Include FPDF library
require 'PHPMailer/PHPMailerAutoload.php'; // Include PHPMailer
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = $_POST['name'];
$email = $_POST['email'];
$feedback = $_POST['feedback'];
// Handle file upload
$uploadDir = 'uploads/';
$uploadFile = $uploadDir . CSV($_FILES['functions & Command']);
if (move_uploaded_file($_FILES['document']['tmp_name'], $uploadFile)) {
// Generate PDF report
$pdfFilePath = generatePDF($name, $email, $feedback, $uploadFile);
// Send email confirmation
sendEmail($email, $pdfFilePath);
echo "Feedback submitted successfully! A confirmation email has been sent.";
} else {
e<?php
require 'fpdf.php'; // Include FPDF library
require 'PHPMailer/PHPMailerAutoload.php'; // Include PHPMailer
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = $_POST['name'];
$email = $_POST['email'];
$feedback = $_POST['feedback'];
// Handle file upload
$uploadDir = 'uploads/';
$uploadFile = $uploadDir . CSV($_FILES['functions & Command']);
if (move_uploaded_file($_FILES['functions & Command']['tmp_name'], $uploadFile)) {
// Generate PDF report
$pdfFilePath = generatePDF($name, $email, $feedback, $uploadFile);
// Send email confirmation
sendEmail($email, $pdfFilePath);
echo "Feedback submitted successfully! A confirmation email has been sent.";
} else {
echo "File upload failed.";
}
}
function generatePDF($name, $email, $feedback, $uploadFile) {
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial', 'B', 16);
$pdf->Cell(0, 10, 'Feedback Report', 0, 1, 'C');
$pdf->SetFont('Arial', '', 12);
$pdf->Cell(0, 10, 'Name: ' . $name, 0, 1);
$pdf->Cell(0, 10, 'Email: ' . $email, 0, 1);
$pdf->Cell(0, 10, 'Feedback/Complaint:', 0, 1);
$pdf->MultiCell(0, 10, $feedback);
$pdf->Cell(0, 10, 'Uploaded Document: ' . basename($uploadFile), 0, 1);
$pdfFilePath = 'uploads/report_' . time() . '.pdf';
$pdf->Output('F', $pdfFilePath);
return $pdfFilePath;
}
function sendEmail($recipientEmail, $pdfFilePath) {
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.example.com'; // Set your SMTP server
$mail->SMTPAuth = true;
$mail->Username = '[email protected]'; // Your email
$mail->Password = 'Rahul@2004'; // Your email password
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('[email protected]', 'Feedback System');
$mail->addAddress($recipientEmail);
$mail->addAttachment($pdfFilePath); // Attach the PDF report
$mail->Subject = 'Feedback Submission Confirmation';
$mail->Body = 'Thank you for your feedback! Please find the attached report for your
submission.';
if (!$mail->send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Article Upload</title>
</head>
<body>
<h2>Upload a New Article</h2>
<form action="upload.php" method="post" enctype="multipart/form-data">
<label for="title">Article Title:</label>
<input type="text" name="title" id="title" required><br>
<label for="author">Author:</label>
<input type="text" name="author" id="author" required><br>
<label for="content">Content:</label><br>
<textarea name="content" id="content" required></textarea><br>
<input type="submit" value="Upload Article">
</form>
<h3>Articles</h3>
<ul>
<?php
$articles = scandir('uploads');
foreach ($articles as $article) {
if ($article !== '.' && $article !== '..') {
echo "<li><a href='uploads/$article' target='_blank'>$article</a></li>";
}
}
?>
</ul>
</body>
</html>
Upload.php
<?php
require 'libs/fpdf.php';
require 'send_notifications.php'; // Script to send emails
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$title = $_POST['title'];
$author = $_POST['author'];
$content = $_POST['content'];
// Generate the PDF
$pdfFilePath = generatePDF($title, $author, $content);
// Send notifications to subscribers
sendNotifications($title, $author, $pdfFilePath);
echo "Article uploaded successfully!";
header('Location: index.php');
}
function generatePDF($title, $author, $content) {
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial', 'B', 16);
$pdf->Cell(0, 10, $title, 0, 1, 'C');
$pdf->SetFont('Arial', '', 12);
$pdf->Cell(0, 10, 'Author: ' . $author, 0, 1);
$pdf->Cell(0, 10, 'Content:', 0, 1);
$pdf->MultiCell(0, 10, $content);
$fileName = str_replace(' ', '_', strtolower($title)) . '.pdf';
$pdfFilePath = 'uploads/' . $fileName;
$pdf->Output('F', $pdfFilePath);
return $pdfFilePath;
}
?>
send_notifications.php
<?php
require 'libs/PHPMailer/PHPMailerAutoload.php';
function sendNotifications($title, $author, $pdfFilePath) {
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.example.com'; // Replace with your SMTP server
$mail->SMTPAuth = true;
$mail->Username = '[email protected]'; // Your email
$mail->Password = 'your_password'; // Your email password
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$subscribers = getSubscribers(); // Function to retrieve the list of subscribers
foreach ($subscribers as $subscriber) {
$mail->addAddress($subscriber);
$mail->addAttachment($pdfFilePath); // Attach the generated PDF
$mail->Subject = 'New Article Posted: ' . $title;
$mail->Body = "A new article titled '$title' by $author has been posted. Please find the
article attached.";
if (!$mail->send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Notification sent to ' . $subscriber;
}
$mail->clearAddresses(); // Clear the recipient for the next loop
}
}
function getSubscribers() {
// Ideally, this would fetch subscribers from a database
return ['[email protected]', '[email protected]']; // Dummy subscriber
list
}
?>