i have pdf file in directory and one php display all file, i want to banned access direct url , the way to achive just click on my php display all file
To restrict direct access to the PDF files and only allow access through your PHP script that displays the files, you can use the following approach:
Move PDF files outside the web root:
Place your PDF files outside the public web directory. For example, if your web root is /var/www/html, move your PDF files to /var/www/pdf_files or another directory that is not accessible via a direct URL.
Update your PHP script:
Modify your PHP script to handle the file display and generate links dynamically. Use a script like the one below:
<?php
$pdfDirectory = '/path/to/pdf_files/';
// Get the list of PDF files
$pdfFiles = glob($pdfDirectory . '*.pdf');
// Display links to the PDF files
foreach ($pdfFiles as $pdfFile) {
$filename = basename($pdfFile);
echo '<a href="view_pdf.php?file=' . urlencode($filename) . '">' . $filename . '</a><br>';
}
?>
In this script, view_pdf.php is a new script that you will create to handle the actual display of PDF files.
Create a new script for handling PDF display (view_pdf.php):
Create a new PHP script (view_pdf.php) to handle the display of PDF files. This script will be responsible for serving the PDF content.
php
Copy code
<?php
$pdfDirectory = '/path/to/pdf_files/';
// Get the requested file from the query parameter
$requestedFile = isset($_GET['file']) ? $_GET['file'] : '';
// Validate the file name to prevent directory traversal
if (preg_match('/^[a-zA-Z0-9_-]+\.pdf$/', $requestedFile) && file_exists($pdfDirectory . $requestedFile)) {
// Set appropriate headers for PDF content
header('Content-Type: application/pdf');
header('Content-Disposition: inline; filename="' . $requestedFile . '"');
// Output the PDF file content
readfile($pdfDirectory . $requestedFile);
} else {
// Invalid or non-existent file requested
echo 'Invalid request.';
}
?>
This script checks if the requested file is a valid PDF file in the specified directory and then serves the content. Adjust the paths and file validation as needed.
With this setup, users will only be able to access the PDF files through your PHP script, preventing direct access to the files via URLs. Additionally, make sure to implement proper security measures, such as input validation and sanitization, to prevent any security vulnerabilities.
เวอไนน์ไอคอร์ส
ประหยัดเวลากว่า 100 เท่า!
เวอไนน์เว็บไซต์⚡️
สร้างเว็บไซต์ ดูแลเว็บไซต์
Categories
- ai (1)
- cat (1)
- computer internet (285)
- food (2)
- med (23)
- Music Movie (31)
- tool (43)
- ฮวงจุ้ย (9)
- Travel (69)
- Uncategorized (1,451)
- web (628)
- Inodes เต็ม
- vss
- vs
- ปัญหา wordpress 2026/2/11
- สร้าง Theme เบาและseoไม่ซับซ้อน
- local storage เปลี่ยนมาใช้ระบบ “อ่าน-เขียนไฟล์”
- ระบบ “เตือนล่วงหน้าด้วยสี” (Color Highlight)
- ปีแตก / premium เกิดแถว / 2926
- สร้างรายได้จาก content ขยะ
- ปัญหา การอัพโหลดjson กับ โปรแกรม v65.html v77.html มีปัญหา
- No One Can Ever Control You If You Learn These 34 Psychology Tricks
- google free AI
- ปุ่มคัดลอก และ ปุ่มลบ มีปัญหา มันจะไปคัดลอกและลบไม่ตรงแถว (โปรแกรมกรีดยางพารา)
- คอลั่ม paid ไม่อ่านค่าใน local storage
- ทำไม เมื่อปิด browser brave แล้วเปิดใหม่ ข้อมูลใน Local storage หาย ครับ
- เพิ่มคอมม่า v69=comma+preview.html
- ใส่ comma ในตัวเลข
- สรุปฟังก์ชันและประโยชน์พิเศษของโปรแกรมตารางบันทึกข้อมูลลูกค้า
- code v66 vs v66-1 ต่างกันอย่างไร
- ข้อมูล Local storage หายไปเอง