snipt

Ctrl+h for KB shortcuts

PHP

Prompt user to 'open' or 'save' file when delivering dynamic file data with PHP

1
2
3
4
5
6
7
<?
header("content-type: $mime_type");
header("content-disposition: attachment; filename=$file_name");
header("cache-control: private");
header("pragma: private");
echo $file_data;
?>
https://snippets.siftie.com/embed/e107659ef24a3df68f6f7f9e131ae206/
/raw/e107659ef24a3df68f6f7f9e131ae206/
e107659ef24a3df68f6f7f9e131ae206
php
PHP
7
2016-12-30T02:33:35
True
False
False
/api/public/snipt/14226/
prompt-user-to-open-or-save-when-delivering-file-data-stored-in-database-with-php
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="cp">&lt;?</span> </span><span id="L-2"><a name="L-2"></a><span class="nb">header</span><span class="p">(</span><span class="s2">&quot;content-type: </span><span class="si">$mime_type</span><span class="s2">&quot;</span><span class="p">);</span> </span><span id="L-3"><a name="L-3"></a><span class="nb">header</span><span class="p">(</span><span class="s2">&quot;content-disposition: attachment; filename=</span><span class="si">$file_name</span><span class="s2">&quot;</span><span class="p">);</span> </span><span id="L-4"><a name="L-4"></a><span class="nb">header</span><span class="p">(</span><span class="s2">&quot;cache-control: private&quot;</span><span class="p">);</span> </span><span id="L-5"><a name="L-5"></a><span class="nb">header</span><span class="p">(</span><span class="s2">&quot;pragma: private&quot;</span><span class="p">);</span> </span><span id="L-6"><a name="L-6"></a><span class="k">echo</span> <span class="nv">$file_data</span><span class="p">;</span> </span><span id="L-7"><a name="L-7"></a><span class="cp">?&gt;</span><span class="x"></span> </span></pre></div> </td></tr></table>
download, file, headers, http, php