SoWizardly
SoWizardly

Reputation: 407

Laravel 5 - Download remote file with dynamic FTP info

I can't seem to find any information about how one would download a remote file w/ Laravel 5 via FTP.

I tried using standard PHP

ftp_connect();

and I was met with this error...

Call to undefined function App\Http\Controllers\Tools\ftp_connect()

Is their a way to use this function, or is this something I should use a part of Laravel for?

I looked into using some part of Laravel for this, and most of the examples assume that you know the FTP ahead of time to update a config file somewhere...but the FTP info I use is given dynamically, so I guess I can't use that? Unless I'm supposed to use Config::set somehow?

I'm a little lost, any help is greatly appreciated!

Upvotes: 2

Views: 6715

Answers (2)

Leandro Perini
Leandro Perini

Reputation: 384

This is how you create dynamic FTP drivers and use it.

$ftp = Storage::createFtpDriver([
                                 'host'     => 'ftp.example.com.br',
                                 'username' => 'ftpuser',
                                 'password' => 'password',
                                 'port'     => '21',
                                 //'root'     => '',
                                 //'passive'  => '',
                                 //'ssl'      => '',
                                 'timeout'  => '30',
                             ]); 
$filename = 'path/filename.txt'; 
$ftp->put($filename, 'some file content to write'); 
$filecontent = $ftp->get($filename);

Upvotes: 11

Rai
Rai

Reputation: 696

You're probably in a namespaced class, so PHP is looking for a ftp_connect() function inside that namespace (App\Http\Controllers\Tools).
To jump back to the global namespace, just add a backslash before the function:

\ftp_connect();

Upvotes: 2

Related Questions