PFZ.nl - PHP Community: word bestand openen vanaf link /sql-php - Offtopic - Forum - PFZ.nl - PHP Community

Je kunt niet antwoorden op dit topic
#1 16-01-2012 13:09
  • P. Idzinga
  • Groep: Forumleden
  • Posts: 98
  • Actief sinds: 25-10-2010
Ik heb een tabel die een overzicht geeft van alle documenten die op mijn externe hardeschijf staan. Als ik deze met een link wil openen wil hij het bestand opslaan ipv. gelijk in word openen. Kan iemand mij vertellen hoe ik dit kan oplossen?

#2 16-01-2012 13:28
Wellicht heb je wat aan deze functie:

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
75
76
77
78
    function dl_file($file) {


        //First, see if the file exists
        if (!is_file($file)) {
            die("<b>404 File not found!</b>");
        }

        //Gather relevent info about file
        $len = filesize($file);
        $filename2 = basename($file);
        $file_extension = strtolower(substr(strrchr($filename2, "."), 1));


        //This will set the Content-Type to the appropriate setting for the file
        switch ($file_extension) {
            case "pdf":
                $ctype = "application/pdf";
                break;
            case "txt":
                $ctype = "text/plain";
                break;
            case "doc":
                $ctype = "application/msword";
                break; //Warning! writes path in file
            case "docx":
                $ctype = "application/msword";
                break; //Warning! writes path in file
            case "xls":
                $ctype = "application/vnd.ms-excel";
                break;
            case "xlsx":
                //$ctype = "application/vnd.ms-excel";
                $ctype = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                break;
            case "ppt":
                $ctype = "application/vnd.ms-powerpoint";
                break;
            case "gif":
                $ctype = "image/gif";
                break;
            case "png":
                $ctype = "image/png";
                break;
            case "jpeg":
            case "jpg":
                $ctype = "image/jpeg";
                break;

                //The following are for extensions that shouldn't be downloaded (sensitive stuff, like php files)
            case "php":
            case "htm":
            case "html":
                die("<b>Cannot be used for " . $file_extension . " files!</b>");
                break;

            default:
                $ctype = "application/force-download";
        }

        //Begin writing headers
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Cache-Control: public");
        header("Content-Description: File Transfer");
        
        //Use the switch-generated Content-Type
        header("Content-Type: $ctype");

        //Force the download
        $header = "Content-Disposition: attachment; filename=" . $filename2 . ";";
        //header($header);
        header("Content-Transfer-Encoding: binary");
        header("Content-Length: " . $len);
        @readfile($file);
        exit;
    }
Zie ook onze vacature op PFZ.nl voor een junior developer!

#3 16-01-2012 13:33
Php kan je hier niet mee niet helpen. Dat zal eerder een instelling van de browser/computer zijn. Je kunt met php alleen forceren dat een browser de "download"-knop laat zien. Maar direct openen zal toch echt afhankelijk van de browser/computer zijn?

Lijkt me anders niet echt veilig, als je met php computerprogramma's van je bezoekers kunt bedienen...

#4 16-01-2012 15:53
  • Martin P
  • Groep: Moderators
  • Posts: 3645
  • Actief sinds: 19-04-2007

Bekijk Post Op 16-01-2012 14:28 schreef Frank Pietersen (cre8it.nl):

1
2
3
4
        //First, see if the file exists
        if (!is_file($file)) {
            die("<b>404 File not found!</b>");
        }
Je vergeet hier de basis van een 404-pagina... een 404-header. Nu geef je een "200 OK"-header en een 404-melding als tekst.
Handleidingen zijn er niet voor niets, gebruik ze dus :)
HTML5 ~ CSS ~ PHP ~ MySQL ~ SQL-injectie bestaat meer dan 13 jaar

#5 16-01-2012 18:38
@martin,
je hebt gelijk, dat moet beter...
Zie ook onze vacature op PFZ.nl voor een junior developer!


Inloggen wachtwoord vergeten? Aanmelden