หลังจาก upload แล้วจะ generate password ให้ สำหรับ pdf นั้น
ปิด broswer เมื่อไร ทั้งหมดหาย (ไม่ได้อัพโหลดเข้าระบบจริง)
<?php
session_start();
// Set the path where PDF files will be stored
$uploadPath = 'uploads/';
// Function to generate a random password
function generatePassword($length = 8) {
return substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);
}
// Function to hash the password
function hashPassword($password) {
return password_hash($password, PASSWORD_DEFAULT);
}
// Function to verify the entered password
function verifyPassword($password, $hashedPassword) {
return password_verify($password, $hashedPassword);
}
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['upload'])) {
// Handle file upload
if (isset($_FILES['pdfFile']) && $_FILES['pdfFile']['error'] === UPLOAD_ERR_OK) {
$password = generatePassword();
$hashedPassword = hashPassword($password);
// Set session variables for password and file path
$_SESSION['password'] = $hashedPassword;
$_SESSION['filePath'] = $uploadPath . $_FILES['pdfFile']['name'];
move_uploaded_file($_FILES['pdfFile']['tmp_name'], $uploadPath . $_FILES['pdfFile']['name']);
echo 'PDF file uploaded successfully! Password: ' . $password;
} else {
echo 'Error uploading the PDF file.';
}
}
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['download'])) {
// Handle file download
$enteredPassword = $_POST['password'];
if (isset($_SESSION['password']) && verifyPassword($enteredPassword, $_SESSION['password'])) {
$filePath = $_SESSION['filePath'];
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="' . basename($filePath) . '"');
readfile($filePath);
exit;
} else {
echo 'Incorrect password. Please try again.';
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PDF Upload and Download</title>
</head>
<body>
<h1>PDF Upload and Download</h1>
<!-- File upload form -->
<form action="" method="post" enctype="multipart/form-data">
<label for="pdfFile">Upload PDF File:</label>
<input type="file" name="pdfFile" id="pdfFile" accept=".pdf" required>
<button type="submit" name="upload">Upload</button>
</form>
<hr>
<!-- File download form with password -->
<form action="" method="post">
<label for="password">Enter Password:</label>
<input type="password" name="password" id="password" required>
<button type="submit" name="download">Download PDF</button>
</form>
</body>
</html>
เพิ่ม โชว์pathไฟล์ ด้วย หลัง upload
<?php
session_start();
$uploadPath = 'uploads/';
function generatePassword($length = 8) {
return substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);
}
function hashPassword($password) {
return password_hash($password, PASSWORD_DEFAULT);
}
function verifyPassword($password, $hashedPassword) {
return password_verify($password, $hashedPassword);
}
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['upload'])) {
if (isset($_FILES['pdfFile']) && $_FILES['pdfFile']['error'] === UPLOAD_ERR_OK) {
$password = generatePassword();
$hashedPassword = hashPassword($password);
$_SESSION['password'] = $hashedPassword;
$fileName = $_FILES['pdfFile']['name'];
$_SESSION['filePath'] = $uploadPath . $fileName;
move_uploaded_file($_FILES['pdfFile']['tmp_name'], $uploadPath . $fileName);
echo 'PDF file uploaded successfully! Password: ' . $password . '<br>';
echo 'File URL: <a href="' . $_SESSION['filePath'] . '">' . $_SESSION['filePath'] . '</a>';
} else {
echo 'Error uploading the PDF file.';
}
}
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['download'])) {
$enteredPassword = $_POST['password'];
if (isset($_SESSION['password']) && verifyPassword($enteredPassword, $_SESSION['password'])) {
$filePath = $_SESSION['filePath'];
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="' . basename($filePath) . '"');
readfile($filePath);
exit;
} else {
echo 'Incorrect password. Please try again.';
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PDF Upload and Download</title>
</head>
<body>
<h1>PDF Upload and Download</h1>
<form action="" method="post" enctype="multipart/form-data">
<label for="pdfFile">Upload PDF File:</label>
<input type="file" name="pdfFile" id="pdfFile" accept=".pdf" required>
<button type="submit" name="upload">Upload</button>
</form>
<hr>
<form action="" method="post">
<label for="password">Enter Password:</label>
<input type="password" name="password" id="password" required>
<button type="submit" name="download">Download PDF</button>
</form>
</body>
</html>
โชว์ ไฟล์ทั้งหมดด้วย
<?php
session_start();
$uploadPath = 'uploads/';
function generatePassword($length = 8) {
return substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);
}
function hashPassword($password) {
return password_hash($password, PASSWORD_DEFAULT);
}
function verifyPassword($password, $hashedPassword) {
return password_verify($password, $hashedPassword);
}
// Initialize the list of uploaded files
if (!isset($_SESSION['uploadedFiles'])) {
$_SESSION['uploadedFiles'] = [];
}
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['upload'])) {
if (isset($_FILES['pdfFile']) && $_FILES['pdfFile']['error'] === UPLOAD_ERR_OK) {
$password = generatePassword();
$hashedPassword = hashPassword($password);
$fileName = $_FILES['pdfFile']['name'];
$filePath = $uploadPath . $fileName;
// Store the uploaded file information in the session
$_SESSION['uploadedFiles'][] = [
'fileName' => $fileName,
'filePath' => $filePath,
'password' => $password
];
$_SESSION['password'] = $hashedPassword;
move_uploaded_file($_FILES['pdfFile']['tmp_name'], $uploadPath . $fileName);
echo 'PDF file uploaded successfully! Password: ' . $password . '<br>';
echo 'File URL: <a href="' . $filePath . '">' . $filePath . '</a>';
} else {
echo 'Error uploading the PDF file.';
}
}
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['download'])) {
$enteredPassword = $_POST['password'];
if (isset($_SESSION['password']) && verifyPassword($enteredPassword, $_SESSION['password'])) {
$filePath = $_SESSION['filePath'];
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="' . basename($filePath) . '"');
readfile($filePath);
exit;
} else {
echo 'Incorrect password. Please try again.';
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PDF Upload and Download</title>
</head>
<body>
<h1>PDF Upload and Download</h1>
<form action="" method="post" enctype="multipart/form-data">
<label for="pdfFile">Upload PDF File:</label>
<input type="file" name="pdfFile" id="pdfFile" accept=".pdf" required>
<button type="submit" name="upload">Upload</button>
</form>
<hr>
<form action="" method="post">
<label for="password">Enter Password:</label>
<input type="password" name="password" id="password" required>
<button type="submit" name="download">Download PDF</button>
</form>
<hr>
<!-- Display the list of uploaded files -->
<?php if (isset($_SESSION['uploadedFiles']) && !empty($_SESSION['uploadedFiles'])): ?>
<h2>List of Uploaded Files:</h2>
<ul>
<?php foreach ($_SESSION['uploadedFiles'] as $file): ?>
<li>
<strong><?php echo $file['fileName']; ?></strong>
- Password: <?php echo $file['password']; ?>
- File URL: <a href="<?php echo $file['filePath']; ?>"><?php echo $file['filePath']; ?></a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</body>
</html>