{"id":405,"date":"2022-11-24T13:48:19","date_gmt":"2022-11-24T12:48:19","guid":{"rendered":"https:\/\/luca.lovalvo.ch\/blog\/?p=405"},"modified":"2022-11-24T13:49:33","modified_gmt":"2022-11-24T12:49:33","slug":"php-telecharger-un-fichier-doc-docxodt-corrompu-avec-readfile","status":"publish","type":"post","link":"https:\/\/luca.lovalvo.ch\/blog\/php-telecharger-un-fichier-doc-docxodt-corrompu-avec-readfile\/","title":{"rendered":"PHP T\u00e9l\u00e9charger un fichier doc,  docx, odt &#8211; corrompu avec readfile()"},"content":{"rendered":"\n<p>Il peut arriver que lors de l&rsquo;appel au t\u00e9l\u00e9chargement d&rsquo;un fichier de type docx ou odt, par exemple, qu&rsquo;\u00e0 l&rsquo;ouverture le fichier soit corrompu.<\/p>\n\n\n\n<p>La solution consiste \u00e0 ajouter avant l&rsquo;envoi des headers, \u00ab\u00a0ob_end_clean();\u00a0\u00bb<\/p>\n\n\n\n<p>Exemple :<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><br>$FileName=\u00a0\u00bbmonfichier.odt\u00a0\u00bb; <br>$FilePath=&rsquo;..\/dossier_securise\/&rsquo;;<br>header(&lsquo;Content-Type: &lsquo;.filetype( $FilePath ));<br>header(&lsquo;Content-Disposition:attachment;filename=&rsquo;.$FileName); <br>readfile($FilePath);<\/p>\n<\/blockquote>\n\n\n\n<p>Devient : <\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-medium-pink-color\">ob_end_clean();<\/mark><br>$FileName=\u00a0\u00bbmonfichier.odt\u00a0\u00bb; <br>$FilePath=&rsquo;..\/dossier_securise\/&rsquo;;<br>header(&lsquo;Content-Type: &lsquo;.filetype( $FilePath ));<br>header(&lsquo;Content-Disposition:attachment;filename=&rsquo;.$FileName); <br>readfile($FilePath);<\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Il peut arriver que lors de l&rsquo;appel au t\u00e9l\u00e9chargement d&rsquo;un fichier de type docx ou odt, par exemple, qu&rsquo;\u00e0 l&rsquo;ouverture le fichier soit corrompu. La solution consiste \u00e0 ajouter avant l&rsquo;envoi des headers, \u00ab\u00a0ob_end_clean();\u00a0\u00bb Exemple : $FileName=\u00a0\u00bbmonfichier.odt\u00a0\u00bb; $FilePath=&rsquo;..\/dossier_securise\/&rsquo;;header(&lsquo;Content-Type: &lsquo;.filetype( $FilePath ));header(&lsquo;Content-Disposition:attachment;filename=&rsquo;.$FileName); readfile($FilePath); Devient : ob_end_clean();$FileName=\u00a0\u00bbmonfichier.odt\u00a0\u00bb; $FilePath=&rsquo;..\/dossier_securise\/&rsquo;;header(&lsquo;Content-Type: &lsquo;.filetype( $FilePath ));header(&lsquo;Content-Disposition:attachment;filename=&rsquo;.$FileName); readfile($FilePath);<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-405","post","type-post","status-publish","format-standard","hentry","category-programmation"],"_links":{"self":[{"href":"https:\/\/luca.lovalvo.ch\/blog\/wp-json\/wp\/v2\/posts\/405","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/luca.lovalvo.ch\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/luca.lovalvo.ch\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/luca.lovalvo.ch\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/luca.lovalvo.ch\/blog\/wp-json\/wp\/v2\/comments?post=405"}],"version-history":[{"count":2,"href":"https:\/\/luca.lovalvo.ch\/blog\/wp-json\/wp\/v2\/posts\/405\/revisions"}],"predecessor-version":[{"id":408,"href":"https:\/\/luca.lovalvo.ch\/blog\/wp-json\/wp\/v2\/posts\/405\/revisions\/408"}],"wp:attachment":[{"href":"https:\/\/luca.lovalvo.ch\/blog\/wp-json\/wp\/v2\/media?parent=405"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/luca.lovalvo.ch\/blog\/wp-json\/wp\/v2\/categories?post=405"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/luca.lovalvo.ch\/blog\/wp-json\/wp\/v2\/tags?post=405"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}