0% found this document useful (0 votes)
30 views

Aman-PHP Assignment-3

Notes for php

Uploaded by

goyalvarun563
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Aman-PHP Assignment-3

Notes for php

Uploaded by

goyalvarun563
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Assignment -3

Name – Aman Kumar Rajak


Roll no-53

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 file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}

// 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");

// Send email confirmation


$to = "[email protected]"; // Replace with user's email
$subject = "File Uploaded Successfully";
$message = "Your file " . basename($_FILES["fileToUpload"]["name"]) . " has
been successfully uploaded on " . $upload_time;
$headers = "From: [email protected]";

mail($to, $subject, $message, $headers);

// Redirect to list of files with success message


header("Location: list_files.php?upload_success=1&time=" .
urlencode($upload_time));
} else {
echo "Sorry, there was an error uploading your file.";
}
}
}
?>
List_file.php
<?php
$dir = "uploads/";

if (isset($_GET['upload_success']) && $_GET['upload_success'] == 1) {


echo "<p style='color:green;'>File uploaded successfully at " . $_GET['time'] . "</p>";
}

$files = scandir($dir);

if (count($files) > 2) { // Because '.' and '..' are also returned


echo "<h2>Uploaded Files</h2>";
echo "<ul>";
foreach ($files as $file) {
if ($file != "." && $file != "..") {
echo "<li><a href='download.php?file=" . urlencode($file) . "'>$file</a></li>";
}
}
echo "</ul>";
} else {
echo "<p>No files uploaded yet.</p>";
}
?>

<a href="index.php">Go back to upload page</a>

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>

<label for="message">Your Feedback or Complaint:</label><br>


<textarea name="message" rows="5" cols="30" required></textarea><br>

<label for="file">Upload Supporting Document (optional):</label>


<input type="file" name="file"><br>

<input type="submit" name="submit" value="Submit">


</form>
</body>
</html>

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 = '';

// Handle file upload


if ($_FILES['file']['name']) {
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["file"]["name"]);
$fileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));

if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
$filePath = $target_file;
} else {
echo "There was an error uploading the file.";
$uploadOk = 0;
}
}

// Generate PDF Report


$pdf = new TCPDF();
$pdf->AddPage();
$pdf->SetFont('Helvetica', '', 12);
$pdf->Cell(0, 10, 'Feedback/Complaint Summary', 0, 1, 'C');
$pdf->Ln(10);
$pdf->Cell(0, 10, "Name: $name", 0, 1);
$pdf->Cell(0, 10, "Email: $email", 0, 1);
$pdf->MultiCell(0, 10, "Message: $message", 0, 1);

if ($filePath) {
$pdf->Cell(0, 10, "Attached Document: " . basename($filePath), 0, 1);
}
$pdfFile = 'uploads/feedback_' . time() . '.pdf';
$pdf->Output($pdfFile, 'F');

// Send Email with PDF attachment


$mail = new PHPMailer;
$mail->setFrom('[email protected]', 'Feedback System');
$mail->addAddress($email, $name);
$mail->Subject = 'Feedback/Complaint Submission Confirmation';
$mail->Body = "Thank you for your submission, $name.\nPlease find the
summary of your submission attached.";
$mail->addAttachment($pdfFile);

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';

$mail = new PHPMailer;

$mail->isSMTP(); // Set mailer to use SMTP


$mail->Host = 'smtp.example.com'; // Specify main and backup SMTP
servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '[email protected]'; // SMTP username
$mail->Password = 'your-email-password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also
accepted
$mail->Port = 587; // TCP port to connect to

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>

<label for="content">Article Content:</label><br>


<textarea name="content" rows="10" cols="50" required></textarea><br><br>

<label for="author">Author Email:</label>


<input type="email" name="author" required><br><br>
<input type="submit" name="submit" value="Submit Article">
</form>

<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']);

// Save the article to a text file


$articleFile = 'articles/' . time() . '.txt';
file_put_contents($articleFile, "Title: $title\n\n$content");

// Generate a PDF version of the article


$pdf = new TCPDF();
$pdf->AddPage();
$pdf->SetFont('Helvetica', '', 12);
$pdf->Cell(0, 10, "Article: $title", 0, 1, 'C');
$pdf->Ln(10);
$pdf->MultiCell(0, 10, $content, 0, 1);

$pdfFile = 'pdfs/article_' . time() . '.pdf';


$pdf->Output($pdfFile, 'F');

// Send Email Notification


$mail = new PHPMailer;
$mail->setFrom('[email protected]', 'Article Platform');
$mail->addAddress($authorEmail);
$mail->Subject = 'New Article Submitted: ' . $title;
$mail->Body = "Your article '$title' has been successfully submitted. You can
download it in PDF format below.";
$mail->addAttachment($pdfFile);

// Also notify admin (you can add more recipients here)


$mail->addAddress('[email protected]'); // Replace with admin's email

if ($mail->send()) {
echo "Article submitted successfully! A notification email has been sent.";
} else {
echo "Error in sending email: " . $mail->ErrorInfo;
}

// Redirect to the article list


header("Location: list_articles.php?success=1");
}
?>

list_articles.php
<?php
$dir = "articles/";

echo "<h2>List of Articles</h2>";

if (isset($_GET['success']) && $_GET['success'] == 1) {


echo "<p style='color:green;'>Article submitted successfully!</p>";
}

$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]);

// Display article with a link to download its PDF


echo "<li><strong>$title</strong> - <a href='download_pdf.php?file=" .
urlencode(basename($file)) . "'>Download PDF</a></li>";
}
}
echo "</ul>";
} else {
echo "<p>No articles available yet.</p>";
}

echo "<br><a href='index.php'>Submit a New Article</a>";


?>
download_pdf.php
<?php
if (isset($_GET['file'])) {
$file = urldecode($_GET['file']);
$pdfFile = "pdfs/article_" . pathinfo($file, PATHINFO_FILENAME) . ".pdf";
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 = new PHPMailer;

$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;

You might also like