Laravel 7. How to download a file from server (FTP)? - laravel

I created a disk in config/filesystems.php file
'ftp' => [
'driver' => 'ftp',
'host' => 'ftp.domain.org',
'username' => 'username',
'password' => 'password',
'passive' => true,
'timeout' => 30,
'root' => '/',
'url' => '/'
],
The connection is tested and works. On the server this file exists:
$file_path = "/folder/aaa.txt";
But I can't download it! In the controller I wrote:
$file = Storage::disk('ftp')->download($file_path);
return response()->download($file);
And this is the outcome:
Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException
The file "HTTP/1.0 200 OK Cache-Control: no-cache, private
Content-Disposition: attachment; filename=aaa.txt Content-Length: 0
Content-Type: text/plain Date: Fri, 10 Jul 2020 19:20:51 GMT"
does not exist
Additional issue
Instead of downloading it, how do I display the same file in the browser? The following code does not work in this case:
return response()->file($file_path);

Both response()->download($file) and Storage::download($file) will create a download response so you only need one of the two. Since your file is in remote storage you can just keep:
return Storage::disk('ftp')->download($file_path);
You can also customise the filename and headers. You can also (probably) make the file show inline by doing:
return Storage::disk('ftp')->download($file_path, 'any.txt', [
'Content-Disposition' => 'inline'
]);

Related

Laravel 9 sFTP Unable to write file at location text.txt. not able to write the file

I am trying to create a file on a sFtp server:
Storage::disk('backup')->put('text.txt', 'My Text');
Unfortunately, I only get the following error message back, which I can't do anything with:
League\Flysystem\UnableToWriteFile
Unable to write file at location: text.txt. not able to write the file`
I have checked the permissions for the directory, they are ok.
My Filesystem config:
'backup' => [
'driver' => 'sftp',
'host' => env('SFTP_HOST'),
// Settings for basic authentication...
'username' => env('SFTP_USERNAME'),
'password' => env('SFTP_PASSWORD'),
'throw' => true,
],

Laravel not making Guzzle HTTP POST request correctly

I am working on a megento integration and trying to get the admin access token by making a post request with form data. I tested the route out on Postman and it worked correctly:
However, when I tried to implement the same request in Laravel with Guzzle Http Client, it seem just cannot make the request properly as if the form data post body is not being recognized, and it keeps showing me errors saying the field values are required. Here is my request:
$client = new \GuzzleHttp\Client();
$response = $client->post($request['magento_domain'] . '/rest/V1/integration/admin/token', [
'form_params' => [
'username' => $magento_admin_username,
'password' => $magento_admin_password
], [
'Accept' => 'application/json',
'Content-Type' => 'application/json'
]
]);
and then this is the error I keep getting:
Update: I had also tried the request like this, it throws the same error:
$response = $client->post($request['magento_domain'] . '/rest/V1/integration/admin/token', [
'form_params' => [
'username' => $magento_admin_username,
'password' => $magento_admin_password
]
]);
I would appreciate any help!
The "Content-Type: application/json" request header is incorrect when sending a form-data body.
Just remove it, Guzzle automatically adds the correct Content-Type when using "form_params". Just JSON is wrong because the body is obviously not JSON.
I am using a JSON request in production successfully:
$res = $this->client->request('POST', 'https://.../rest/V1/integration/admin/token', [
'headers' => [
'Accept' => 'application/json',
'content-type' => 'application/json'
],
'json' => [
'username' => config('app.shopUser'),
'password' => config('app.shopPw')
]
]);
Or try using "multipart" instead of "form_params" - this should send a multipart/form-data request which is what Postman means with "form-data".
"form_params" is equivalent to "x-www-form-urlencoded".

Laravel FTP - Root path for windows server

I am trying to upload a file from my local to FTP server using laravel
'ftp' => [
'driver' => 'ftp',
'host' => env('FTP_HOST'),
'username' => env('FTP_USERNAME'),
'password' => env('FTP_PASSWORD'),
'port' => '21',
'passive' => false,
'root' => 'c:/POSActiveAutoInsert/WEBSales_In/' // for example: /var/www/html/dev/images
//
],
In my controller
Storage::disk('ftp')->put('test.txt', $file);
But when I run this, it says
Root is invalid or does not exist: c:/POSActiveAutoInsert/WEBSales_In/
I have tried FTP using Filezilla it works fine
The FTP account I am using takes you directly to c:/POSActiveAutoInsert/WEBSales_In/ this folder. This WEBSales_In is the root folder for this FTP user that I am using. Can't figure out what the root path would be, or am I doing something wrong other than this.
Cheers
You can try this in your controller
$path = $request->your_variable_name->storeAs('text', '$file', ['disk' => 'ftp']);

Upload file FTP with Filesystem Laravel error

I try to upload a video file to my download server with FTP file system in laravel but I have a this error:
League\Flysystem\ConnectionRuntimeException: Could not connect to host: myip, port:21 in file /home/test/domains/myhost/laravel/vendor/league/flysystem/src/Adapter/Ftp.php on line 140
in my filesytem.php
'ftp' => [
'driver' => 'ftp',
'host' => 'myip',
'username' => 'myusername',
'password' => 'mypass',
'port' => 21,
],
how to fix this error
FTP port:21 is for pass control information.
Did you try port:20 ?
im using sftp because when i using ftp is error in port too. and don't forget too enable ftp in php.ini
'sftp' => [
'driver' => 'sftp',
'host' => 'xxx.xxx.com',
'username' => 'user',
'password' => 'pass',
'root' => ' xxx' , // for example: /public_html/images
I fix this issue by enabling an FTP extension in my server.

Laravel 5 : Ajax response download not working

Tried below code:
$headers = [
'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
'Content-type' => 'text/csv',
'Content-Disposition' => 'attachment; filename=list.csv',
'Expires' => '0',
'Pragma' => 'public'
];
$list = List::all()->toArray();
# add headers for each column in the CSV download
array_unshift($list, array_keys($list[0]));
$FH = fopen(storage_path('list.csv'),'w');
foreach ($list as $row) {
fputcsv($FH, $row);
}
fclose($FH);
$path = storage_path('list.csv');
return response()->download($path, 'list.csv', $headers);
Below will out put in inspect element network call
I have got the same issue in Stack overflow but not working for me.
Exporting CSV response laravel 5.5 and download as csv file
Can have any idea regarding this what I have missing in here?

Resources