I am new to laravel. I am making an application in which I check a directory that is being served on an SFTP. By looking into the documentation I was successfully able to list all the file names inside a directory hosted on that SFTP.
My next goal is to save all those in a local folder public/InputFiles
but how do I do that? I have tried several combinations but none has helped.
Here is what I have done. In config/filesystem.php
I have
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => public_path().'/InputFiles',
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
'sftp' => [
'driver' => 'sftp',
'host' => 'exampleHost.com',
'username' => 'username',
'password' => 'password',
]
In my controller I have this function
public function getFilesFromFtp()
{
//all files are present inside 'outgoing' folder
$file_list = Storage::disk('sftp')->allFiles('outgoing/');
foreach ($file_list as $key => $value) {
# code...
//output the name of the files
$this->printVariable(str_replace("outgoing/", "", $value));
Storage::disk('public')->put(str_replace("outgoing/", "", $value), Storage::disk('sftp')->download($value));
}
For example I had a file named as sample.txt
inside the outgoing folder on SFTP. Inside this file I had text as Sample text
.
By using the above method, I was able to see the sample.txt
file made in desired location but its contents were as follows
HTTP/1.0 200 OK
Cache-Control: no-cache, private
Content-Disposition: attachment; filename="sample.txt"
Content-Length: 12
Content-Type: text/plain
Date: Thu, 19 Apr 2018 10:35:10 GMT
This is not what I want. I actually want to save the file as it is just like on SFTP. How do I do that?
Just changed the line to
Storage::disk('public')->put(str_replace("outgoing/", "", $value), Storage::disk('sftp')->get($value));
So basically I am getting file from sftp and putting it in local directory. No need to download it.