I have a function that generates an mpg file. And i want this to automatically prompt you to download the file. This is the code that i have and i can't figure out how to make it instantly prompt to download this mpg file and save or open it. If someone could help me that would be awsome. if($format == "mpg"){ $filename = download($download,$movieid,$the_path); convert_to_mpg($filename,$movieid); header("Content-Type: application/force-download"); header("Content-Disposition: attachment; filename=\"".{$filename}."\""); } Code (markup):
It seems like you have misinterpreted the Content-type header. (or not quite, more on the end) When you send header("Content-Type: type/subtype"); you indicate the type and subtype of the content about to be sent by the HTTP protocol, also known as MIME type (or sometimes Internet media type). In other words; your document are "transformed" into another type and tells the receiving part/the client how to interpret the data about to come. Multipurpose Internet Mail Extensions (MIME) is an extention for e-mail. Makes it possible to transfere non ASCII and so forth. HTTP has adapted the content types defined by MIME standards. (to explain it short) Mime types are important for the server as well PHP has application/x-httpd-php as default. If you run Apache you add the line: AddType application/x-httpd-php .php when configuring it for php. Apache also have a long list of known mimes in a file called mime.types. If you in the config file write AddType application/x-httpd-php .phelixx you can save your php files as .phelixx instead of .php and it will be parsed as a php file. (Nite huh? At IANA we find a list of MIME media types: http://www.iana.org/assignments/media-types/ |-application- |-audio------- |-image------- |-media-types-|-message----- |-model------- |-multipart--- |-text-------- |-video------- Code (markup): Here you can dig trough hours of MIME specifications.. I.e.: Video -> mpeg -> RFC2045 and RFC2046 So. When you want to send a mpeg file you have to specify correct Content-type. For mpeg this is video/mpeg. From RFC2046 we have: 4.4. Video Media Type A media type of "video" indicates that the body contains a time- varying-picture image, possibly with color and coordinated sound. The term 'video' is used in its most generic sense, rather than with reference to any particular technology or format, and is not meant to preclude subtypes such as animated drawings encoded compactly. The subtype "mpeg" refers to video coded according to the MPEG standard [MPEG]. Code (markup): You can find a lot of lists for MIME types on the net. Simply search for it. I.e.: you have this one; http://www.utoronto.ca/webdocs/HTMLdocs/Book/Book-3ed/appb/mimetype.html or http://www.w3schools.com/media/media_mimeref.asp Here we find that .mpg has type/subtype; video/mpeg. After you have defined the Content-type and other header information you send the data in the file; here being the data in the .mpg file. I.e.: <?php // Informing it is video/mpeg header('Content-type: video/mpeg'); // Informing that it will be attachment and the sugested filename is videoclip.mpg header('Content-disposition: attachment; filename=videoclip.mpg'); // Output the data-file, if it is in a file you can do; readfile('videoclip.mpg'); // But you can also generate it, store it in a database and so on. ?> PHP: Additional headers are many. Recommend you to i.e. specify size of data to be transmitted. And remember. Headers can't be sent if any data has been sent to the client. (I.e.: echo('Hello'); Code (markup): ) or <html><head>... Code (markup): ) DO not even have a "new-line" before <?php. But, you can have the code and "page" in same file; i.e. (Here I also have included some more header values making it more likely it won't bug in IE(who often try to define MIME for itself instead of reading headers hrmf..) and that file will be re-read each time) <?php if(isset($_GET["dlfile"])){ $file = $_GET["dlfile"]; if(is_file($file)){ if(ini_get('zlib.output_compression')) ini_set('zlib.output_compression', 'Off'); header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: private",false); header('Content-type: video/mpeg'); header("Content-Length: ".(string)(filesize($file))); header("Content-Transfer-Encoding: binary"); header("Content-disposition: attachment; filename=\"".$file."\";"); readfile($file); exit(); } } ?> < a href="<?php echo $_SERVER["PHP_SELF"]; ?>?dlfile=larry_the_cable_guy.mpg">Larry The Cable Guy</a > PHP: Ref: MIME type lists: http://www.utoronto.ca/webdocs/HTMLdocs/Book/Book-3ed/appb/mimetype.html http://www.w3schools.com/media/media_mimeref.asp http://www.iana.org/assignments/media-types/ More about: http://www.mhonarc.org/~ehood/MIME/ http://no.php.net/manual/en/function.header.php .. and of course RFC's in general... * In the code you posted you use Content-Type: application/force-download force-download is a experimental sub-type. If you read the RFC's i think this should be named x-force-download. Would also think it can be buggy in some browsers. You can use application/octet-stream if you prefer that way...