I want to upload a file from Laravel to another server using FTP.
It seems a very simple task, so let's take a look at my configurations:
.env file
FTP_HOST=dl.myserver.com
FTP_USERNAME=beni#dl.myserver.com
FTP_PASSWORD=somePass
filesystem.php
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'ftp' => [
'driver' => 'ftp',
'host' => env('FTP_HOST'),
'username' => env('FTP_USERNAME'),
'password' => env('FTP_PASSWORD'),
'passive' => true,
'port' => 21,
'root' => '/home/myserver/public_html/podcasts'
],
.
.
.
and my controller finally
$year = Carbon::now()->year;
$month = Carbon::now()->month;
$day = Carbon::now()->day;
//podcast
$podcast = $request->file('podcast');
$filename = $podcast->getClientOriginalName();
$purename = substr($filename, 0, strrpos($filename, '.'));
$filenametostore = $purename . '_' . $year .'_' . $month . '_' . $day . '.' . $podcast->getClientOriginalExtension();
Storage::disk('ftp')->put($filenametostore, fopen($request->file('podcast'), 'r+'));
.
.
.
but I have this error:
League\Flysystem\ConnectionRuntimeException
Could not log in with connection: dl.myserver.com::21, username:
beni#dl.myserver.com
My FTP account and information is true because I logged in using FileZilla.
As a mention, my dl.server.com is using CPANEL.
Is there any Idea about this issue?
Thanks in Advance
You need to put the password with double quotes in your .env
Particularly if your password contains spaces or #
FTP_PASSWORD="some#Pass"
Surprisingly the problem solved when I replaced env('FTP_HOST'), env('FTP_USERNAME') and env('FTP_PASSWORD') with equivalent string values in filesystems.php file!
I tried pure PHP FTP functions and figured it out:
$conn_id = ftp_connect("dl.myserver.com");
ftp_login($conn_id, "beni#dl.myserver.com", "somePass");
dd(ftp_put($conn_id, $filenametostore, $request->file('podcast'), FTP_ASCII));
So my Laravel filesystem.php looks like this:
'ftp' => [
'driver' => 'ftp',
'host' => "dl.myserver.com", //env('FTP_HOST'),
'username' => "beni#dl.myserver.com", //env('FTP_USERNAME'),
'password' => "somePass", //env('FTP_PASSWORD'),
],
and it works fine in my case.
Related
I have managed to configure my sftp in the 'config/filesystems.php' file:
'remote-sftp' => [
'driver' => 'sftp',
'host' => env('SFTP_HOST'),
'username' => env('SFTP_USERNAME'),
'password' => env('SFTP_PASSWORD'),
'root' => '/var/www/html/files/current',
'visibility' => 'public',
'permPublic' => 0644,
'timeout' => 30,
],
An sftp account was created on the remote server (usert) and I successfully manage to upload the file to the server, but, the problem is that the files is being stored in the wrong folder. My code to upload the file :
$fileName = $id_number . '_tps_' . $datetime . '.' . $request->file('file')->extension();
Storage::disk('remote-sftp')->put($fileName, fopen($request->file('file'), 'r+'), 'public');
I expect the file to be stored in '/var/www/html/files/current' as specified in 'config/filesystems.php' but i find the file is actually stored in '/home/usert'. How to I get it to save in the '/var/www/html/files/current'?
example image response error File not found at path
hey can you help me?
so here I want to view the image file from the ftp server that will be responded to by JS along with the FTP server link
controller example :
$explode = explode('#',$lampiran->lampiran_gambar);
foreach($explode as $row){
if($row == null){
$row1[] = 'null';
}else{
$row1[] = Storage::disk('ftp')->get('/lampiranSurat' . $row);
}
}
if($pegawai_pejabat->jenis_jabatan_id == 1){
return response()->json([
'meta' => [
'code' => 200,
'status' => 'success',
'message' => 'Data Ditemukan',
],
'data_verifikasi' => $verifikasi,
'lampiran_gambar' => $row1,
'pegawai_verif' => $pegawai_verif,
]);
}
example config filesystem :
'default' => env('FILESYSTEM_DRIVER', 'ftp')
'ftp' => [
'driver' => 'ftp',
'host' => env('FTP_HOST'),
'username' => env('FTP_USERNAME'),
'password' => env('FTP_PASSWORD'),
'root' => '/web',
],
config file .env :
FTP_HOST=exampleftpserver.com
FTP_USERNAME=userftp
FTP_PASSWORD=password123
so why is my ftp url not being read in storage?
for image file data already in the database and already in FTP
You're missing a / between your directory name and the filename:
Replace
$row1[] = Storage::disk('ftp')->get('/lampiranSurat' . $row);
With
$row1[] = Storage::disk('ftp')->get('/lampiranSurat/' . $row);
I am trying to upload a file to a public folder which was working lately but now it is showing below error:
Disk [public] does not have a configured driver.
I tried checking for configured driver in config/filesystems.php but, it is already set there. I am not getting where the issue might be.
Upload code:
public function upload(ProductImageRequest $request, Product $product)
{
$image = $request->file('file');
$dbPath = $image->storePublicly('uploads/catalog/'.$product->id, 'public');
if ($product->images === null || $product->images->count() === 0) {
$imageModel = $product->images()->create(
['path' => $dbPath,
'is_main_image' => 1, ]
);
} else {
$imageModel = $product->images()->create(['path' => $dbPath]);
}
return response()->json(['image' => $imageModel]);
}
Code in config/filesystems.php
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
i use this code for moving the picture and storing its name you may want to give it a shot
//get icon path and moving it
$iconName = time().'.'.request()->icon->getClientOriginalExtension();
$icon_path = '/category/icon/'.$iconName;
request()->icon->move(public_path('/category/icon/'), $iconName);
$category->icon = $icon_path;
i usually move the image then store its path in db and this is what my code shows you can edit it as desired
I'm working on a Drupal 8 starter kit with Composer, similar to drupal-composer/drupal-project.
In my post-install script, I want to re-generate a settings.php file with my custom values.
I've seen that can be done with the drupal_rewrite_settings function.
For example, I'm rewriting the config_sync_directory value like that :
require_once $drupalRoot . '/core/includes/bootstrap.inc';
require_once $drupalRoot . '/core/includes/install.inc';
new Settings([]);
$settings['settings']['config_sync_directory'] = (object) [
'value' => '../config/sync',
'required' => TRUE,
];
drupal_rewrite_settings($settings, $drupalRoot . '/sites/default/settings.php');
Problem is I want my Drupal 8 project to have a Dotenv so the maintainers don't have to modify the settings.php but only a .env file in the root folder of the project. To make it work, my settings.php must look like this :
$databases['default']['default'] = [
'database' => getenv('MYSQL_DATABASE'),
'driver' => 'mysql',
'host' => getenv('MYSQL_HOSTNAME'),
'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
'password' => getenv('MYSQL_PASSWORD'),
'port' => '',
'prefix' => '',
'username' => getenv('MYSQL_USER'),
];
$settings['trusted_host_patterns'] = explode(',', '^'.getenv('SITE_URL').'$');
As you can see, the values are replaced by PHP functions, and I can't see a good way to print those values, to the point I'm not even sure that's possible.
So my question is : is it possible to escape a PHP function as an Array value when declaring this variable ?
Looks like it's not possible because of the way the Drupal function works.
Solution 1 by #misorude
Using the drupal_rewrite_settings function, we can add the value of settings as a String, like this :
$settings['settings']['trusted_host_patterns'] = (object) [
'value' => "FUNC[explode(',', '^'.getenv('SITE_URL').'$')]",
'required' => TRUE,
];
And after that, we can replace all occurrences of "FUNC[***]" by *** directly in the settings.php file.
Solution 2
Put all your settings in a separate file. Example here, a custom.settings.php file :
if (getenv('DEBUG') == 'true') {
$settings['container_yamls'][] = DRUPAL_ROOT . '/sites/dev.services.yml';
$config['system.performance']['css']['preprocess'] = FALSE;
$config['system.performance']['js']['preprocess'] = FALSE;
}
$databases['default']['default'] = [
'database' => getenv('MYSQL_DATABASE'),
'driver' => 'mysql',
'host' => getenv('MYSQL_HOSTNAME'),
'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
'password' => getenv('MYSQL_PASSWORD'),
'port' => '',
'prefix' => '',
'username' => getenv('MYSQL_USER'),
];
$settings['trusted_host_patterns'] = explode(',', '^'.getenv('SITE_URL').'$');
$settings['file_private_path'] = 'sites/default/files/private';
$settings['config_sync_directory'] = '../config/sync';
Then we can copy the default.settings.php and add our custom settings.
$fs = new Filesystem();
$settings_generated = $drupalRoot . '/sites/default/settings.php';
$settings_default = $drupalRoot . '/sites/default/default.settings.php';
$settings_custom = $drupalRoot . '/../includes/custom.settings.php';
$fs->remove($settings_generated);
$fs->dumpFile($settings_generated, file_get_contents($settings_default) . file_get_contents($settings_custom));
There's also a appendToFile method that seems way better than dumping a new file with dumpFile, but it was not working unfortunatly.
I am having some troubles with laravels filesystem uploading.
When I try to execute this code
Storage::disk('public')->put(
$img->getClientOriginalName(),
file_get_contents($img->getRealPath())
);
nothing happens locally in the public folder, I even checked if the file exists and it returns true
dd(Storage::disk('public')->exists($img->getClientOriginalName()));
For now I am using the $img->move method and it works as I want to.
Disk is also configured in filesystems.php
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'visibility' => 'public',
],
I am confused with this because a couple of weeks ago it worked as it should on another project.
I have now fixed this problem with the help of Claudio by using 'root' => public_path('').
This should work:
if ($request->hasFile('myFile'))
{
$fileExtension = strtolower($request->file('myFile')->getClientOriginalExtension());
$newFilename = str_random(20) . '.' . $fileExtension;
$storagePath = storage_path() . '/app/uploads/';
$request->file('myFile')->move($storagePath, $newFilename);
}