'it is not possible to upload a file to ftp via the Laravel 8 form
I have a problem uploading a file in Laravel 8.81.0 on ftp. I have this form in the home.blade.php file:
<form action="{{addFile}}" method="post" enctype="multipart/form-data">
@method('PUT')
@csrf
<p>
<input type="file" name="profile_image" />
</p>
<button type="submit" name="submit">Submit</button>
</form>
I have yes in the controller
if($request->hasFile('profile_image')) {
//get filename with extension
$filenamewithextension = $request->file('profile_image')->getClientOriginalName();
//get filename without extension
$filename = pathinfo($filenamewithextension, PATHINFO_FILENAME);
//get file extension
$extension = $request->file('profile_image')->getClientOriginalExtension();
//filename to store
$filenametostore = $filename.'_'.uniqid().'.'.$extension;
//Upload File to external server
Storage::disk('ftp')->put($filenametostore, fopen($request->file('profile_image'), 'r+'));
//Store $filenametostore in the database
}
in the .env file I added
FTP_HOST=xxx
FTP_USERNAME=xxx
FTP_PASSWORD=xxx
In the filesystems.php file
'disks' => [
'ftp' => [
'driver' => 'ftp',
'host' => env(xxx),
'username' => env(xxx),
'password' => env('xxx'),
'root' => '/public/' // for example: /var/www/html/dev/images
],
But anyway after uploading the file I get the error: ValueError Path cannot be empty.
I am using file transfer for the first time, so I would be grateful for a hint of what I am doing wrong I mention that directories are set to allow file writing
Solution 1:[1]
'ftp' => [
'driver' => 'ftp',
'host' => 'FTP_HOST',
'username' => 'FTP_USERNAME',
'password' => 'FTP_PASSWORD',
'passive' => 'false',
'ignorePassiveAddress' => true,
'port' => 21,
'root' => 'public_html/storage/app/public/Your_Folder/' // for example: /public_html/images
]
//file will save in storage directory you need to link storage for access your files
$path = $file->store('Your Folder','ftp');
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Muhammad Khalid |
