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

laravel how to store image in 4 different folder at same time

my below code stores image 2 different at the same time but I am trying to store image in 4 different folders at the same time, but it not storing 4 different folder it only passing image in 2 folders how to slow this problem

This code only Stores in Two Folder

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 you provided code

<?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 .');   
}

}

您正在尝试使用只有两个参数的 \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 .');
}

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

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

}

如果您遇到任何问题,请在下方发表评论

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