My code for moving an image to another subdomain public path:
<?php
if ($request->front_picture_path != '') {
$imageName3 = time() . '_front.' . request()->front_picture_path->getClientOriginalExtension();
request()->front_picture_path->move(($path), $imageName3);
$imgdata['right_picture_path'] = public_path('uploads_ext/uploads') . '/' . $imageName3;
}
Currently, my public path is, for example:
/test/abc/test/client12/web/19/web/public/uploads_ext/uploads/1723021012_left.jpg
But I want to move to another subdomain:
/test/abc/test/client12/web21/web/public/uploads_ext/uploads/1723021012_left.jpg
How to achieve this?
My code for moving an image to another subdomain public path:
<?php
if ($request->front_picture_path != '') {
$imageName3 = time() . '_front.' . request()->front_picture_path->getClientOriginalExtension();
request()->front_picture_path->move(($path), $imageName3);
$imgdata['right_picture_path'] = public_path('uploads_ext/uploads') . '/' . $imageName3;
}
Currently, my public path is, for example:
/test/abc/test/client12/web/19/web/public/uploads_ext/uploads/1723021012_left.jpg
But I want to move to another subdomain:
/test/abc/test/client12/web21/web/public/uploads_ext/uploads/1723021012_left.jpg
How to achieve this?
Share Improve this question edited Feb 17 at 11:47 Abdulla Nilam 38.6k18 gold badges68 silver badges95 bronze badges Recognized by PHP Collective asked Feb 17 at 7:29 Jayasri S.VJayasri S.V 214 bronze badges 1 |2 Answers
Reset to default 0You just need to define the absolute path where you want to move the file :
<?php
if ($request->hasFile('front_picture_path')) {
$imageName = time() . '_front.' . $request->file('front_picture_path')->getClientOriginalExtension();
// Get the base path
$basePath = base_path();
// Adjust the path to point to the correct subdomain (web21 instead of web19)
$destinationPath = str_replace('/19/web/', '/21/web/', $basePath) . '/public/uploads_ext/uploads';
// Move the uploaded file
$request->file('front_picture_path')->move($destinationPath, $imageName);
// Store the new file path
$imgdata['right_picture_path'] = $destinationPath . '/' . $imageName;
}
We can use scp command for moving files from current to destination directory.
if ($request->hasFile('front_picture_path')) {
$file = $request->file('front_picture_path');
$filename = $file->getClientOriginalName();
// Store temporarily in storage
$tempPath = $file->storeAs('temp', $filename);
$remoteUser = 'myusername';
$remoteHost = 'myhost';
$remotePath = "mypath";
$password = 'mypassword';
$scpCommand = "scp " . storage_path("app/{$tempPath}") . " {$remoteUser}@{$remoteHost}:{$remotePath}";
shell_exec($scpCommand);
If you are using a password,then create a Pem file and transfer files using pem key without passowrd.
/data/upload
– Abdulla Nilam Commented Feb 17 at 11:35