<-- Terug naar Snippets

PHP Downloadscript

PHP versie: >= 4

Javascript: n.v.t

Voorbeeld van een downloadscript. Houd de locatie waar de bestanden staan door bij een download enkel dit script aan te roepen. Het bestand wordt meegegeven in een GET variabelen en het script biedt de download aan de gebruiker aan.

Downloads

Broncode

Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
ini_set
('display_errors''On');
error_reporting(E_ALL);


function 
getFile($sLocation$sName)
{
    if(!
file_exists($sLocation) || strstr($sLocation'../'))
    {
        echo 
"<script>alert('Dit bestand bestaat niet.');</script>";
    }
    else
    {    
        
header ('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        
header ('Content-Description: File Transfer');
        
header ('Content-Type: application/octet-stream');
        
header ('Content-Length: ' filesize($sLocation));
        
header ('Content-Disposition: attachment; filename=' basename($sName));
        
readfile($sLocation);
    }
}

$sDir 'downloadscript/';
if(isset(
$_GET['file']))
{
    
$sFile trim($_GET['file']);
    if(!empty(
$sFile))
    {
        
getFile($sDir.$sFile$sFile);
    }
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>PHPtuts.nl - PHP Snippets - PHP Downloadscript</title>
    
    <link rel="stylesheet" href="../../styles/default.css" type="text/css" media="screen" />
    <link rel="stylesheet" href="../../styles/ubb.css" type="text/css" media="screen" />
    
</head>
<body>
    <div id="wrap">
        <p class="none small align-right"><a href="/codeSnippets/">&lt;-- Terug naar Snippets</a></p>
        <h1>PHP Downloadscript</h1>
        <div id="info">
            <p>PHP versie: &gt;= 4</p>
            <p>Javascript: n.v.t</p>
        </div>
        
        <p>Voorbeeld van een downloadscript. Houd de locatie waar de bestanden staan door bij een download enkel dit script aan te roepen. Het bestand wordt meegegeven in een GET variabelen en het script biedt de download aan de gebruiker aan.</p>
        
        <h3>Downloads</h3>
        <ul>
            <li><a href="downloadscript.php?file=bestand_1.txt">Bestand 1</a></li>
            <li><a href="downloadscript.php?file=bestand_2.txt">Bestand 2</a></li>
            <li><a href="downloadscript.php?file=bestand_3.txt">Bestand 3</a></li>
        </ul>
        
        <?php
        
// Weergeven broncode
        
define('FILE'__FILE__);
        require_once(
'../broncode.php');
        
?>
    </div>    

<!-- Google analytics (geen onderdeel van script) -->
<script type="text/javascript" src="/inc/ga1.js"></script>
<script type="text/javascript" src="/inc/ga2.js"></script>

</body>
</html>