Welcome Guest, Not a member yet? Register   Sign In
How to download a file in codeigniter using download helper?
#1

[eluser]Unknown[/eluser]
Download a file

Hi, I need a php code to download a file from a website and before downloading,it should give me an option to browse and select the location to put the file. Thanks in advance.Sandip Ray

N.B. : Totally new in PHP & CodeIgniter.Pls help.
#2

[eluser]Unknown[/eluser]
You can use download_helper :
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* CodeIgniter
*
* An open source application development framework for PHP 4.3.2 or newer
*
* @package  CodeIgniter
* @author  ExpressionEngine Dev Team
* @copyright Copyright (c) 2008 - 2010, EllisLab, Inc.
* @license  http://ellislab.com/codeigniter/user-guide/license.html
* @link  http://codeigniter.com
* @since  Version 1.0
* @filesource
*/

// ------------------------------------------------------------------------

/**
* CodeIgniter Download Helpers
*
* @package  CodeIgniter
* @subpackage Helpers
* @category Helpers
* @author  ExpressionEngine Dev Team
* @link  http://ellislab.com/codeigniter/user-guide/helpers/download_helper.html
*/

// ------------------------------------------------------------------------

/**
* Force Download
*
* Generates headers that force a download to happen
*
* @access public
* @param string filename
* @param mixed the data to be downloaded
* @return void
*/
if ( ! function_exists('force_download'))
{
function force_download($filename = '', $data = '')
{
  if ($filename == '' OR $data == '')
  {
   return FALSE;
  }

  // Try to determine if the filename includes a file extension.
  // We need it in order to set the MIME type
  if (FALSE === strpos($filename, '.'))
  {
   return FALSE;
  }

  // Grab the file extension
  $x = explode('.', $filename);
  $extension = end($x);

  // Load the mime types
  @include(APPPATH.'config/mimes'.EXT);

  // Set a default mime if we can't find it
  if ( ! isset($mimes[$extension]))
  {
   $mime = 'application/octet-stream';
  }
  else
  {
   $mime = (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension];
  }

  // Generate the server headers
  if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE"))
  {
   header('Content-Type: "'.$mime.'"');
   header('Content-Disposition: attachment; filename="'.$filename.'"');
   header('Expires: 0');
   header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
   header("Content-Transfer-Encoding: binary");
   header('Pragma: public');
   header("Content-Length: ".strlen($data));
  }
  else
  {
   header('Content-Type: "'.$mime.'"');
   header('Content-Disposition: attachment; filename="'.$filename.'"');
   header("Content-Transfer-Encoding: binary");
   header('Expires: 0');
   header('Pragma: no-cache');
   header("Content-Length: ".strlen($data));
  }

  exit($data);
}
}


/* End of file download_helper.php */
/* Location: ./system/helpers/download_helper.php */

In the controller :
Code:
function donwload(){
$this->load->helper('download');
$data = file_get_contents('path/file.xls');
force_download('file.xls', $data);
}

Hoping to help you




Theme © iAndrew 2016 - Forum software by © MyBB