Comment faire un fichier téléchargeable en PHP ?

Asked  Jan 27, 08:28 am 49 Views Add Your Answer
Asked  Jan 27, 08:28 am
Php
0 Dislikes
0 Comments

1 Answers

Added  Jan 27, 05:03 pm

In PHP, you can use the built-in function header() to send the appropriate headers to the browser, which will prompt the user to download the file. Here is an example of how you can make a file downloadable in PHP:

<?php
    // Set the file path
    $file = 'path/to/file.txt';

    // Check if the file exists
    if (file_exists($file)) {
        // Set the headers
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename="'.basename($file).'"');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        // Read the file
        readfile($file);
        // Exit
        exit;
    }
?>

Know more

0 Likes 0 Dislikes
1 Comments
abakarabdallah02 Merci et c'est quoi ces header(' ') ?
Added  Jan 28, 04:29 pm
×
SignUp with Email
X