如何在SFTP存储中将目录从一个位置移动到另一个位置?



我想在SFTP存储上将文件从一个目录移动到另一个目录

我已经试过了:

$file = new Filesystem();
$moved = $file->moveDirectory($from, $to);

,但我需要目录的完整路径,但有以下SFTP代码它不工作:

$full_path_source = Storage::disk('sftp')->getDriver()->getAdapter()->getPathPrefix('batch_1');

配置→filesystem.php

'sftp' => [
'driver' => 'sftp',
'host' => env('SFTP_HOST'),
'username' => env('SFTP_USERNAME'),
'password' => env('SFTP_PASSWORD'),
'prefix'=>'/var/multilabel/batchprocessing_images/'
],

mycontroller

function upload(Request $request){
$dir = 'batch_'.$number;
$tmp_input_path = 'tmp/'.$dir.'/input';
$tmp_output_path = 'tmp/'.$dir.'/output';
$path = $dir.'/input';
$output_path = $dir.'/output';
Storage::disk('sftp')->makeDirectory($tmp_input_path);
Storage::disk('sftp')->makeDirectory($tmp_output_path);
Storage::disk('sftp')->makeDirectory($path);
Storage::disk('sftp')->makeDirectory($output_path);

foreach (request()->file('files') as $key=>$image) {
Storage::disk('sftp')->put($tmp_input_path,$request->file('files')[$key]);
}
$from = 'tmp/'.$dir;
$to = $dir;
$moved = Storage::disk('sftp')->move($from, $to);
}

上面的代码移动空目录

我如何用文件系统movedirectory方法做到这一点

这是奇怪的非常模糊,令人惊讶的是它不能在Laravel文档和API中找到,但是Storage::move函数不仅适用于文件,但它也适用于目录:

Storage::move($oldDirectoryPath, $newDirectoryPath);

要获得文件的路径,您可以简单地调用:

use IlluminateSupportFacadesStorage;

$path = Storage::path('file.jpg');

文件路径:在文档中找到:https://laravel.com/docs/9.x/filesystem#file-paths

您可以使用path方法来获取给定文件的路径。如果您使用的是本地驱动程序,这将返回文件的绝对路径。如果您使用的是s3驱动程序,这个方法将返回s3桶中文件的相对路径:

相关内容

最新更新