laravel如何同时将图像存储在4个不同的文件夹中

时间:2019-05-29 13:01:04

标签: laravel laravel-5.8

  

我下面的代码同时存储不同的图片2,但我正在尝试   将图片同时存储在4个不同的文件夹中,但不存储4个   不同的文件夹,它只传递2个文件夹中的图像,如何减慢速度   问题

     

此代码仅存储在两个文件夹中

public function store(Request $request)

    {

      $this->validate($request, [


          'image' => 'required|image|mimes:jpeg,png,jpg|max:2048',
          ]);


            $input['image'] = time().'.'.$request->image->getClientOriginalExtension();
            $request->image->move(public_path('Folder-a/'), $input['image']);
            $folder1 = public_path('Folder-1/') . $input['image'];
            $folder2 = public_path(Folder-2/') . $input['image'];
            $folder3 = public_path('Folder-3/') . $input['image'];
            $folder4 = public_path(Folder-4/') . $input['image'];
            \File::copy($folder1, $folder2, $folder3, $folder4 );


        Service::create($input);

        return back()->with('success',' CREATED SUCCESSFULLY .');

    }
  

@ Manojkiran.A您提供的代码

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Service;
use Illuminate\Support\Facades\File;
use saveFilesMultipleTimes;
class ServiceController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function store(Request $request)
{
$this->validate($request, ['image' => 'required|image|mimes:jpeg,png,jpg|max:2048',]);
$mainFolderPath = public_path('Folder-a/');
$folder1 = public_path('Folder-1/');
$folder2 = public_path('Folder-2/');
$folder3 = public_path('Folder-3/'); 
$folder4 = public_path('Folder-4/'); 
$fileObject = $request->image; 
$filenamesWithPath = self::saveFilesMultipleTimes( $fileObject, $mainFolderPath, [$folder1, $folder2, 
$folder3, $folder4]);       
dd($filenamesWithPath);
function saveFilesMultipleTimes( \Illuminate\Http\UploadedFile$requestFile, $mainFolderPath,$paths)
{    
$fileName = $requestFile->getClientOriginalName();    
$movedFile = $requestFile->move($mainFolderPath, $fileName);   
$currentFile = $movedFile->getPathName();     
foreach ( (array) $paths as $eachPath)
{
@mkdir( $eachPath);
@copy($movedFile->getPathName(), $eachPath . $fileName);
$dirctory[] = $eachPath.$fileName;
}
return $dirctory;
} 
Service::create($input);
return back()->with('success',' CREATED SUCCESSFULLY .');   
}

}

2 个答案:

答案 0 :(得分:0)

您正在尝试使用\File::copy方法,该方法只有两个参数-源和目标。因此,您只需要保存文件并根据需要分别复制即可。

https://laravel.com/api/5.8/Illuminate/Filesystem/Filesystem.html#method_copy

public function store(Request $request)
{
    $this->validate($request, [
        'image' => 'required|image|mimes:jpeg,png,jpg|max:2048',
    ]);

    $input['image'] = time().'.'.$request->image->getClientOriginalExtension();

    $folder1 = public_path('Folder-1/');

    $path1 = $folder1 . $input['image']; // path 1

    $request->image->move($folder1, $input['image']); // image saved in first folder

    $path2 = public_path('Folder-2/') . $input['image']; // path 2
    $path3 = public_path('Folder-3/') . $input['image']; // path 3
    $path4 = public_path('Folder-4/') . $input['image']; // path 4

    \File::copy($path1, $path2);
    \File::copy($path1, $path3);
    \File::copy($path1, $path4);

    Service::create($input);

    return back()->with('success',' CREATED SUCCESSFULLY .');
}

答案 1 :(得分:0)

好吧,我尝试了很多最终创建函数的方法

function saveFilesMultipleTimes( \Illuminate\Http\UploadedFile$requestFile, $mainFolderPath,$paths)
{

    $fileName = $requestFile->getClientOriginalName();

    $movedFile = $requestFile->move($mainFolderPath, $fileName);

    $currentFile = $movedFile->getPathName();

    foreach ( (array) $paths as $eachPath) 
    {
        @mkdir( $eachPath);
        @copy($movedFile->getPathName(), $eachPath . $fileName);
        $dirctory[] = $eachPath.$fileName;
    }
    return $dirctory;
}

将其复制并粘贴到您的控制器中

现在如何使用它

$mainFolderPath = public_path('Folder-a/');
$folder1 = public_path('Folder-1/');
$folder2 = public_path('Folder-2/');
$folder3 = public_path('Folder-3/');
$folder4 = public_path('Folder-4/');


$fileObject = $request->image;
$filenamesWithPath = saveFilesMultipleTimes( $fileObject, $mainFolderPath, [$folder1, $folder2, $folder3, $folder4]);

dd($filenamesWithPath);

此dd将给出所有文件名以及路径

希望它的帮助

已用控制器更新

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Service;
use Illuminate\Support\Facades\File;

class ServiceController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function store(Request $request)
    {
        $this->validate($request, ['image' => 'required|image|mimes:jpeg,png,jpg|max:2048',]);
        $input['image'] = time().'.'.$request->image->getClientOriginalExtension();
        $mainFolderPath = public_path('Folder-a/');
        $folder1 = public_path('Folder-1/');
        $folder2 = public_path('Folder-2/');
        $folder3 = public_path('Folder-3/');
        $folder4 = public_path('Folder-4/');
        $fileObject = $request->image;
        $filenamesWithPath = self::saveFilesMultipleTimes($fileObject, $mainFolderPath, [$folder1, $folder2,
        $folder3, $folder4]);

        Service::create($input);  

        return back()->with('success', ' CREATED SUCCESSFULLY .');
    }

    public function saveFilesMultipleTimes(\Illuminate\Http\UploadedFile$requestFile, $mainFolderPath, $paths)
    {
        $fileName = $requestFile->getClientOriginalName();
        $movedFile = $requestFile->move($mainFolderPath, $fileName);
        foreach ((array) $paths as $eachPath) {
            @mkdir($eachPath);
            @copy($movedFile->getPathName(), $eachPath . $fileName);
            $dirctory[] = $eachPath.$fileName;
        }
        return $dirctory;
    }

}

如果您遇到任何问题,请在下面评论

并且如果文件已成功上传四次,请标记我的回答为接受