最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - How to move image to another subdomain's public folder? - Stack Overflow

programmeradmin4浏览0评论

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 Why not map all uploads to a common location? This way: Files won’t be duplicated across different locations. and They will be accessible to all servers, ensuring consistency and reducing storage overhead. Ex: /data/upload – Abdulla Nilam Commented Feb 17 at 11:35
Add a comment  | 

2 Answers 2

Reset to default 0

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

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.

发布评论

评论列表(0)

  1. 暂无评论