Jump to content

send file to browser


glenelkins

Recommended Posts

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.