[PHP] Mime-types bij php readfile

Pagina: 1
Acties:
  • 115 views sinds 30-01-2008
  • Reageer

Onderwerpen


Acties:
  • 0 Henk 'm!

  • SA007
  • Registratie: Oktober 2002
  • Laatst online: 00:04

SA007

Moderator Tweaking
Topicstarter
Heel raar dat ik hier niks over kon vinden, maarja...

Ik heb een script gemaakt wat bestanden haalt uit een dir die buiten de webroot staat, het werkt helemaal op een punt na:

De mime-types worden niet "goed" gezet.

Ik weet dat je met een header de content(mime)-type goe kan zetten, maar het idee is dat als het een html pagina is dat hij "text/html" geeft , maar dat hij bij een plaatje wel iets van "image/gif" doet.

Ik kan wel gewoon een lijst maken met extenties en mime-types, maar ik heb het idee dat hier een betere oplossing voor is (bijvoorbeeld de lijst van apache zelf, of misschien staat hier al ergens een lijst van op internet ofzo?)

Acties:
  • 0 Henk 'm!

Anoniem: 26306

http://nl3.php.net/mime_content_type

Ik hoop dat je webserver PHP 4.3.0 ondersteund en met mime magic support is gecompilet. Deze functie doet een best guess aan de hand van een magic file, zoals die uit de file package (volgens mij).

En hier heb je een lijstje.

[ Voor 15% gewijzigd door Anoniem: 26306 op 16-06-2004 22:21 ]


Acties:
  • 0 Henk 'm!

  • Gomez12
  • Registratie: Maart 2001
  • Laatst online: 17-10-2023
Ff denken, je wil bestanden uit de webroot benaderen (=buiten apache) en tegelijk toch een lijst van apache gebruiken???

Als je via php een bestand verstuurt moet je altijd zelf de mime-types meesturen met een header, omdat apache hier niet meer naar kijkt.AFAIK

Maar hieronder een scriptje ( is gejat, maar heb het script sneller voorhanden dan de website )
PHP:
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php
/**
Copyright (C) 2002 Jason Sheets <jsheets@shadonet.com>.
All rights reserved.

THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
   
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
   
3. Neither the name of the project nor the names of its contributors
   may be used to endorse or promote products derived from this software
   without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
**/

/** 
   Name: PHP MimeType Class

   Version: 1.0
   Date Released: 10/20/02

   Description: This class allows a PHP script to determine the mime type
   a file based on the file extension.  This class is handy for PHP versions
   without the benefit of other tools to determine the mime type.  The class
   defaults to octet-stream so if the file type is not recognized the user
   is presented with a file download prompt.

   NOTES: The mime types for this version are based on Apache 1.3.27
   mime types may change or need to be added in the future, the mime types
   are stored in an array so that an awk or other script can automatically
   generate the code to make the array.

   Usage:

    First an instance of the mimetype class must be created, then the
    getType method should be called with the filename.  It will return
    the mime type, an example follows.
    
   Example:

      $mimetype = new mimetype();
      print $mimetype->getType('acrobat.pdf');

   Author: Jason Sheets <jsheets@shadonet.com>

   License: This script is distributed under the BSD License, you are free
   to use, or modify it however you like.  If you find this script useful please
   e-mail me.   
**/

class mimetype {
   function getType($filename) {
      // get base name of the filename provided by user
      $filename = basename($filename);

      // break file into parts seperated by .
      $filename = explode('.', $filename);

      // take the last part of the file to get the file extension
      $filename = $filename[count($filename)-1];   

      // find mime type
      return $this->privFindType(strtolower($filename));
   }

   function privFindType($ext) {
      // create mimetypes array
      $mimetypes = $this->privBuildMimeArray();
      
      // return mime type for extension
      if (isset($mimetypes[$ext])) {
         return $mimetypes[$ext];
      // if the extension wasn't found return octet-stream         
      } else {
         return 'application/octet-stream';
      }
         
   }

   function privBuildMimeArray() {
      return array(
         "ez" => "application/andrew-inset",
         "hqx" => "application/mac-binhex40",
         "cpt" => "application/mac-compactpro",
         "doc" => "application/msword",
         "xls" => "application/excel",
         "bin" => "application/octet-stream",
         "dms" => "application/octet-stream",
         "lha" => "application/octet-stream",
         "lzh" => "application/octet-stream",
         "exe" => "application/octet-stream",
         "class" => "application/octet-stream",
         "so" => "application/octet-stream",
         "dll" => "application/octet-stream",
         "oda" => "application/oda",
         "pdf" => "application/pdf",
         "ai" => "application/postscript",
         "eps" => "application/postscript",
         "ps" => "application/postscript",
         "smi" => "application/smil",
         "smil" => "application/smil",
         "wbxml" => "application/vnd.wap.wbxml",
         "wmlc" => "application/vnd.wap.wmlc",
         "wmlsc" => "application/vnd.wap.wmlscriptc",
         "bcpio" => "application/x-bcpio",
         "vcd" => "application/x-cdlink",
         "pgn" => "application/x-chess-pgn",
         "cpio" => "application/x-cpio",
         "csh" => "application/x-csh",
         "dcr" => "application/x-director",
         "dir" => "application/x-director",
         "dxr" => "application/x-director",
         "dvi" => "application/x-dvi",
         "spl" => "application/x-futuresplash",
         "gtar" => "application/x-gtar",
         "hdf" => "application/x-hdf",
         "js" => "application/x-javascript",
         "skp" => "application/x-koan",
         "skd" => "application/x-koan",
         "skt" => "application/x-koan",
         "skm" => "application/x-koan",
         "latex" => "application/x-latex",
         "nc" => "application/x-netcdf",
         "cdf" => "application/x-netcdf",
         "sh" => "application/x-sh",
         "shar" => "application/x-shar",
         "swf" => "application/x-shockwave-flash",
         "sit" => "application/x-stuffit",
         "sv4cpio" => "application/x-sv4cpio",
         "sv4crc" => "application/x-sv4crc",
         "tar" => "application/x-tar",
         "tcl" => "application/x-tcl",
         "tex" => "application/x-tex",
         "texinfo" => "application/x-texinfo",
         "texi" => "application/x-texinfo",
         "t" => "application/x-troff",
         "tr" => "application/x-troff",
         "roff" => "application/x-troff",
         "man" => "application/x-troff-man",
         "me" => "application/x-troff-me",
         "ms" => "application/x-troff-ms",
         "ustar" => "application/x-ustar",
         "src" => "application/x-wais-source",
         "xhtml" => "application/xhtml+xml",
         "xht" => "application/xhtml+xml",
         "zip" => "application/zip",
         "au" => "audio/basic",
         "snd" => "audio/basic",
         "mid" => "audio/midi",
         "midi" => "audio/midi",
         "kar" => "audio/midi",
         "mpga" => "audio/mpeg",
         "mp2" => "audio/mpeg",
         "mp3" => "audio/mpeg",
         "aif" => "audio/x-aiff",
         "aiff" => "audio/x-aiff",
         "aifc" => "audio/x-aiff",
         "m3u" => "audio/x-mpegurl",
         "ram" => "audio/x-pn-realaudio",
         "rm" => "audio/x-pn-realaudio",
         "rpm" => "audio/x-pn-realaudio-plugin",
         "ra" => "audio/x-realaudio",
         "wav" => "audio/x-wav",
         "pdb" => "chemical/x-pdb",
         "xyz" => "chemical/x-xyz",
         "bmp" => "image/bmp",
         "gif" => "image/gif",
         "ief" => "image/ief",
         "jpeg" => "image/jpeg",
         "jpg" => "image/jpeg",
         "jpe" => "image/jpeg",
         "png" => "image/png",
         "tiff" => "image/tiff",
         "tif" => "image/tif",
         "djvu" => "image/vnd.djvu",
         "djv" => "image/vnd.djvu",
         "wbmp" => "image/vnd.wap.wbmp",
         "ras" => "image/x-cmu-raster",
         "pnm" => "image/x-portable-anymap",
         "pbm" => "image/x-portable-bitmap",
         "pgm" => "image/x-portable-graymap",
         "ppm" => "image/x-portable-pixmap",
         "rgb" => "image/x-rgb",
         "xbm" => "image/x-xbitmap",
         "xpm" => "image/x-xpixmap",
         "xwd" => "image/x-windowdump",
         "igs" => "model/iges",
         "iges" => "model/iges",
         "msh" => "model/mesh",
         "mesh" => "model/mesh",
         "silo" => "model/mesh",
         "wrl" => "model/vrml",
         "vrml" => "model/vrml",
         "css" => "text/css",
         "html" => "text/html",
         "htm" => "text/html",
         "asc" => "text/plain",
         "txt" => "text/plain",
         "rtx" => "text/richtext",
         "rtf" => "text/rtf",
         "sgml" => "text/sgml",
         "sgm" => "text/sgml",
         "tsv" => "text/tab-seperated-values",
         "wml" => "text/vnd.wap.wml",
         "wmls" => "text/vnd.wap.wmlscript",
         "etx" => "text/x-setext",
         "xml" => "text/xml",
         "xsl" => "text/xml",
         "mpeg" => "video/mpeg",
         "mpg" => "video/mpeg",
         "mpe" => "video/mpeg",
         "qt" => "video/quicktime",
         "mov" => "video/quicktime",
         "mxu" => "video/vnd.mpegurl",
         "avi" => "video/x-msvideo",
         "movie" => "video/x-sgi-movie",
         "ice" => "x-conference-xcooltalk"
      );
   }
}

?>

[ Voor 25% gewijzigd door Gomez12 op 16-06-2004 22:23 . Reden: [/php] toegevoegd ]


Acties:
  • 0 Henk 'm!

Anoniem: 26306

Ik wil daar even wat over zeggen. Wat een irritant script. Dat is totaal niet lekker onderhoudbaar met zoveel array entries die wat mij betreft in een los configuratiebestand horen te staan. Je zou wellicht zelfs de apache mime.magic file kunnen gebruiken hiervoor. Een bestand analyseren en waarden in een array stoppen stelt niet zoveel voor. En een tekstbestandje parsen is zo gebeurd.

Acties:
  • 0 Henk 'm!

  • SA007
  • Registratie: Oktober 2002
  • Laatst online: 00:04

SA007

Moderator Tweaking
Topicstarter
Anoniem: 26306 schreef op 16 juni 2004 @ 22:20:
http://nl3.php.net/mime_content_type

Ik hoop dat je webserver PHP 4.3.0 ondersteund en met mime magic support is gecompilet. Deze functie doet een best guess aan de hand van een magic file, zoals die uit de file package (volgens mij).

En hier heb je een lijstje.
tnx, ik heb wel mime-magic, maar ding kan de magic file niet vinden, dus ik heb (wat ook op die pagina stond) dit gebruikt:

PHP:
1
2
3
 function mime_content_type ($file) {
  return exec ("file -bi " . escapeshellcmd($file));
 }


en dat werkt prima

Acties:
  • 0 Henk 'm!

  • Gomez12
  • Registratie: Maart 2001
  • Laatst online: 17-10-2023
Anoniem: 26306 schreef op 16 juni 2004 @ 22:29:
Ik wil daar even wat over zeggen. Wat een irritant script. Dat is totaal niet lekker onderhoudbaar met zoveel array entries die wat mij betreft in een los configuratiebestand horen te staan. Je zou wellicht zelfs de apache mime.magic file kunnen gebruiken hiervoor. Een bestand analyseren en waarden in een array stoppen stelt niet zoveel voor. En een tekstbestandje parsen is zo gebeurd.
LOL, is ook niet mijn script. Maar ik had dit probleem gisteren ook. Toen ik ging zoeken kwam ik op dit script uit Uiteindelijk zijn alle mime-entries gewoon in de dbase beland die ik daar wel vanuit ophaal en de controle is een stuk verbeterd omdat op extensie controleren toch best wel naief is.

Maar was meer bedoelt als een simpel feit over hoe ik de meeste scripts gebruik. Om een idee op te doen / te krijgen.

* Gomez12 heeft nog geen enkel script gevonden wat [ME=24897]gewoon zou willen copy pasten in zijn eigen scripts.[/ME] Over het algemeen is het gewoon kijken wat doet dat script.

Oh dat doet dit, dat kan ik zelf ook wel, alleen dan gebruik ik mijn eigen globale temp directory, oh daar resized hij een bestand. Dat kan ik ook alleen wil ik hem dan ook gelijk nog eens gelogd hebben en het eindresultaat is gewoon weer een script vanaf scratch geschreven met als voorbeeld een ander script...
Pagina: 1