Jayasri S.V
Jayasri S.V

Reputation: 21

How to move image to another subdomain's public folder?

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?

Upvotes: 1

Views: 81

Answers (2)

Jayasri S.V
Jayasri S.V

Reputation: 21

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.

Upvotes: 0

JulBeg
JulBeg

Reputation: 271

You 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;
}

Upvotes: 0

Related Questions