glenelkins Posted June 20, 2009 Share Posted June 20, 2009 hi how do you send a file to the browser for download with php? Quote Link to comment Share on other sites More sharing options...
Mark Baker Posted June 20, 2009 Share Posted June 20, 2009 You use header() to send the appropriate headers for the file tyep that you're sending to the browser. Quote Link to comment Share on other sites More sharing options...
glenelkins Posted June 20, 2009 Author Share Posted June 20, 2009 can you give an example of downloading a file with unknown file type Quote Link to comment Share on other sites More sharing options...
glenelkins Posted June 20, 2009 Author Share Posted June 20, 2009 check out the "get" function, it downloads the file say "image.jpg" but when i open the jpg, the file is empty <?php class Upload_model extends Model { var $id; var $filename; var $user_id; var $hash; function save( $file, $user_id, $hash ) { $this->filename = $file; $this->user_id = $user_id; $this->hash = $hash; $this->db->insert ( 'files', $this ); } function get () { $result = $this->db->get_where ( 'files', array ( 'hash' => $this->hash ) ); if ( $result->num_rows() ) { // Get row $row = $result->row(); // Build file path $this->load->config ( 'paths' ); // $path = $this->config->item ( 'upload_path' ) . $row->user_id . '/' . $row->filename; // send to browser if ( file_exists ( $path ) ) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename='.basename($path)); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-Length: ' . filesize($path)); ob_clean(); flush(); readfile($file); return ( true ); } else { return ( false ); } //die ( $row->filename . ' ' . $row->user_id ); } } } ?> Quote Link to comment Share on other sites More sharing options...
glenelkins Posted June 20, 2009 Author Share Posted June 20, 2009 my bad typo error Quote Link to comment Share on other sites More sharing options...
Mark Baker Posted June 20, 2009 Share Posted June 20, 2009 You're doing readfile($file). What is the value of $file? Everywhere else in the code you're using $path to reference the file for download Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.