Des Trucs à Web

Menu
  • Accueil
  • Langages
    • Html
    • Javascript
    • PHP
  • Serveurs
    • Apache
  • Bdd
    • Mysql
  • Matériel
    • Eviter les reflets sur un PC en extérieur
  • Outils
    • Avis Wisewand
    • Avis Merlin AI
    • Avis SURFER SEO
    • Avis NeuronWriter
    • Klistair notre avis
    • Pabbly automatisation
    • Gutenkit le constructeur WordPress
    • WordHero AI (lifetime)
    • Rédiger grâce à l’IA
  • Systèmes
    • Windows
  • WordPress
  • Guides VPN
    • Un VPN : pourquoi ? Comment ?
    • Avis NordVPN 2025 ⭐9/10
    • Masquer son IP : méthodes
    • Pourquoi utiliser un VPN ?
    • Utiliser un VPN sur un smartphone, pourquoi ?
    • Quel VPN choisir ?
    • Vérifier si VPN sur mon Mobile
    • Fuite DNS : C’est quoi ?
  • Dossiers
Home
Langages
PHP
Forcer le téléchargement d’un fichier en PHP

Forcer le téléchargement d’un fichier en PHP

Forcer le téléchargement d'un fichier en PHP

 

Voici comment forcer le téléchargement d’un fichier en PHP.
Pour que le script fonctionne, il ne faut pas d’instruction qui affiche quelque chose à l’écran avant les lignes header.

Sinon vous obtiendrez un message du style :
 » Warning : Cannot modify header information – headers already sent by (output started at d:\creation\easyphp\www\_test\test.php:2) in d:\creation\easyphp\www\_test\test.php on line 3″

Par exemple créer un fichier » telecharger.php » avec comme contenu :

<?php//Nous essayons de reconnaitre l’extension pour que le téléchargement corresponde au type de

// fichier. Cela permet d’éviter les erreurs de corruptions
switch(strrchr(basename($Fichier_a_telecharger), « . »)) {
case « .gz »: $type = « application/x-gzip »; break;
case « .tgz »: $type = « application/x-gzip »; break;
case « .zip »: $type = « application/zip »; break;
case « .pdf »: $type = « application/pdf »; break;
case « .png »: $type = « image/png »; break;
case « .gif »: $type = « image/gif »; break;
case « .jpg »: $type = « image/jpeg »; break;
case « .txt »: $type = « text/plain »; break;
case « .htm »: $type = « text/html »; break;
case « .html »: $type = « text/html »; break;
default: $type = « application/octet-stream »; break;

}
header(« Content-disposition: attachment; filename=$Fichier_a_telecharger »);
header(« Content-Type: application/force-download »);
header(« Content-Transfer-Encoding: $type\n »); // Surtout ne pas enlever le \n
header(« Content-Length: « .filesize($chemin . $Fichier_a_telecharger));
header(« Pragma: no-cache »);
header(« Cache-Control: must-revalidate, post-check=0, pre-check=0, public »);
header(« Expires: 0 »);
readfile($chemin . $Fichier_a_telecharger);
?>

Ensuite faire un fichier test.php ou test.html suivant ses préférences.
On appelle le fichier telecharger.php en passant en paramètre le nom du fichier à télécharger ainsi que le chemin. Exemple : 

<html>
<head>
<title>Forcer le téléchargement d’un fichier</title>
<meta http-equiv= »Content-Type » content= »text/html; charset=iso-8859-1″>
</head>
<body>

<?php
// Test du téléchargement d’un fichier avec reconnaissance du type de fichier
echo « <a href=\ »telecharger.php?Fichier_a_telecharger=monfichier.txt&chemin=test/\ »>Télécharger</a> »;
?>

</body>
</html>

Ou simplement si l'on ne veut utiliser que du HTML.

<html>
<head>
<title>Test du téléchargement d'un fichier</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

<a href="telecharger.php?Fichier_a_telecharger=monfichier.txt&chemin=test/">Télécharger</a>

</body>
</html>

 

Share
Tweet
Email
Prev Article

Related Articles

Vérifier si une URL existe en PHP
Dans certains cas, il est nécessaire de vérifier que l’URL, …

Vérifier si une URL existe en PHP

Forcer le téléchargement d’un fichier, d’une image, d’un PDF en PHP
Voici comment forcer téléchargement d’un fichier à l’aide du PHP. …

Forcer le téléchargement d’un fichier, d’une image, d’un PDF en PHP

Articles populaires

    Des Trucs à Web

    Des astuces, des ressources, des tutoriels pour votre site
    • Contact
    • Mentions légales
    • Plan du site
    • Offres Black Friday
    • Cyber Monday

    Ce participe au Programme Partenaires d’Amazon EU, un programme d’affiliation conçu pour permettre à des sites de percevoir une rémunération grâce à la création de liens vers Amazon.fr.

    Copyright © 2026 Des Trucs à Web

    Bloqueur de publicité détecté

    Notre site Web est rendu possible en affichant des publicités en ligne à nos visiteurs. Veuillez envisager de nous soutenir en désactivant votre bloqueur de publicités.

    Refresh
    Nous utilisons des cookies sur notre site Web pour vous offrir l'expérience la plus pertinente en mémorisant vos préférences et vos visites répétées. En cliquant sur "Accepter tout", vous consentez à l'utilisation de TOUS les cookies. Cependant, vous pouvez visiter "Cookies Settings" pour fournir un consentement contrôlé.
    Cookie SettingsTout accepter Tout Rejeter
    Manage consent

    Privacy Overview

    This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
    Necessary
    Toujours activé
    Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
    CookieDuréeDescription
    cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
    cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
    cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
    cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
    cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
    viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
    Functional
    Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
    Performance
    Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
    Analytics
    Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
    Advertisement
    Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
    Others
    Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
    Enregistrer & appliquer