Download file in php

            $file  = 'sample.zip';
            $file_path  = $file;
            $path_parts = pathinfo( $file_path );
            $file_name  = $path_parts['basename'];
            $file       = @fopen( $file_path,"rb" );
             if ( $file )
                            {
                                header("Cache-Control: public");
                                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");
                                header("Content-Type: application/zip");
                                header("Content-Disposition: attachment; filename=\"{$file_name}\"");
                                header("Content-Transfer-Encoding: binary");
                                header("Content-Length: " . filesize($file_path));
                                while(!feof($file))
                                    {
                                        echo fread($file, 500*1024);
                                        flush();
                                        if (connection_status()!=0)
                                            {
                                                @fclose($file);
                                                die();
                                            }
                                    }
                                @fclose($file);
                                exit;
                            }

Tags: 

Comments

Woah! I'm really loving the template/theme of this site. It's simple, yet effective. A lot of times it's difficult to get that "perfect balance" between user friendliness and visual appeal. I must say you have done a excellent job with this. In addition, the blog loads super fast for me on Chrome. Outstanding Blog!

X