Downloading file from ftp in codeigniter 2 using FTP Class

265 views Asked by At

I want to download some file from FTP Server using FTP Class that already in CodeIgniter, but i get error ftp_get() failed to open stream : permission denied when i called my code. anyone can help me, please ?

this is my code

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class C_FTP extends CI_Controller 
{
public function openfile($fileName)
{
    $this->load->library('ftp');
    $config['hostname'] = 'ftp_host';
    $config['username'] = 'ftp_username';
    $config['password'] = 'ftp_password';
    $config['port']     = 'ftp_port';
    $config['debug']    = FALSE;
    $config['passive']  = FALSE;
    $this->ftp->connect($config);
    $this->ftp->download('path/to/folder/', 'local/path', 'auto');
    $this->ftp->close();
}}
?>

and my ci version is : 2.2.0

1

There are 1 answers

0
Daffa Al On

I have found the solution of my problem, just add header before you download the file this my code :

before

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class C_FTP extends CI_Controller 
{
public function openfile($fileName)
{
    $this->load->library('ftp');
    $config['hostname'] = 'ftp_host';
    $config['username'] = 'ftp_username';
    $config['password'] = 'ftp_password';
    $config['port']     = 'ftp_port';
    $config['debug']    = FALSE;
    $config['passive']  = FALSE;
    $this->ftp->connect($config);
    $this->ftp->download('path/to/folder/', 'local/path', 'auto');
    $this->ftp->close();
}}
?>

change with this


<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class C_FTP extends CI_Controller 
{
public function openfile($fileName)
{
    $this->load->library('ftp');
    $config['hostname'] = 'ftp_host';
    $config['username'] = 'ftp_username';
    $config['password'] = 'ftp_password';
    $config['port']     = 'ftp_port';
    $config['debug']    = FALSE;
    $config['passive']  = FALSE;
    $this->ftp->connect($config);
    header('Content-type: text/plain');
    header('Content-Disposition: attachment; filename="filename.pdf"');
    $this->ftp->download('path/to/folder/', 'local/path', 'auto');
    $this->ftp->close();
}}
?>