Demonyowh
Demonyowh

Reputation: 1672

PHPSeclib Unable to Download File even tho it exist

I can't seem to understand why the file is not downloading while it really exists. Here's my code.

fileDownload.PHP

use \Logic\FTPConnection;
$ftpPath= 'path/to/file.txt';
$localPath = 'temp/file.txt';

$objFTPCon = new FTPConnection(FTP_HOST, FTP_USER, FTP_PASS);
$objFTPCon->openConnection();
$exist = $objFTPCon->isFileExist($ftpPath); // this returns as true
$objFTPCon->downloadFile($localPath , $ftpPath); // not downloading

FTPConnection.php

public function openConnection() // DONE
{
    if (!$this->ftpOpen) {
        
        $this->objConn = new SFTP($this->strHost);
        if ($this->objConn === false) {
            throw new \Exception('Unable to connect to SFTP server.');
        }

        if (!$this->objConn->login($this->strUser, $this->rsa)) {
            throw new \Exception('Unable to login to SFTP server.');
        }
        $this->ftpOpen = true;
    }
}

public function isFileExist($p_strFile) 
{
    $this->openConnection();
    $strStandardizedPath = $this->standardizePath($p_strFile);

    $blnFileExist = false;
    
    $strFile = substr($strStandardizedPath, strrpos($strStandardizedPath, '/') + 1);
    $strPath = substr($strStandardizedPath, 0, strrpos($strStandardizedPath, '/'));
    
    if ($this->isPathExist($strPath)) {
        $arrFileList = $this->objConn->nlist($strPath);
        if ($arrFileList) {
            if (in_array($strFile, $arrFileList)) {
                $blnFileExist = true;
            }
        }
    }
    
    return $blnFileExist;
}

public function downloadFile($p_strTargetFile, $p_strSourceFile)
{
    $this->openConnection();
    // $this->objConn->get($p_strTargetFile, $p_strSourceFile, FTP_BINARY);
    if (!$this->objConn->get($p_strTargetFile, $p_strSourceFile, FTP_BINARY)) {
        throw new \Exception('Unable to download file.');
    }
}

Upvotes: 1

Views: 208

Answers (1)

Demonyowh
Demonyowh

Reputation: 1672

It appears that my parameters for get() is wrong. I just swapped it and it works:

so instead of:

$this->objConn->get($p_strTargetFile, $p_strSourceFile, FTP_BINARY);

i used:

$this->objConn->get($p_strSourceFile, $p_strTargetFile);

Upvotes: 2

Related Questions