glenelkins Posted June 20, 2009 Share Posted June 20, 2009 hi how do you send a file to the browser for download with php? Link to comment https://forums.phpfreaks.com/topic/163007-send-file-to-browser/ 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. Link to comment https://forums.phpfreaks.com/topic/163007-send-file-to-browser/#findComment-860081 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 Link to comment https://forums.phpfreaks.com/topic/163007-send-file-to-browser/#findComment-860085 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 ); } } } ?> Link to comment https://forums.phpfreaks.com/topic/163007-send-file-to-browser/#findComment-860087 Share on other sites More sharing options...
glenelkins Posted June 20, 2009 Author Share Posted June 20, 2009 my bad typo error Link to comment https://forums.phpfreaks.com/topic/163007-send-file-to-browser/#findComment-860088 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 Link to comment https://forums.phpfreaks.com/topic/163007-send-file-to-browser/#findComment-860121 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.