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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.