• January 11, 2024
    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


Uncategorized