首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何同时将图像存储在4个不同的文件夹中

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

Stack Overflow用户
提问于 2019-05-29 13:01:04
回答 2查看 1.2K关注 0票数 0

下面的代码同时存储不同的图像2,但我试图同时将图像存储在4个不同的文件夹中,但它没有存储4个不同的文件夹--它只在两个文件夹中传递图像--如何减缓这个问题 此代码仅存储在两个文件夹中

代码语言:javascript
复制
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你提供了代码

代码语言:javascript
复制
<?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 .');   
}

}

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-05-29 13:56:23

好的,我已经尝试过很多方法,任何最终创建这个函数的方法

代码语言:javascript
复制
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;
}

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

现在如何使用它

代码语言:javascript
复制
$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将为提供所有文件名以及路径

希望能帮上忙

用控制器更新的

代码语言:javascript
复制
<?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;
    }

}

如果你面临任何问题,请在下面发表评论

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

票数 0
EN

Stack Overflow用户

发布于 2019-05-29 13:41:15

您正在尝试使用\File::copy方法,它只有两个args --源和目的地。因此,您应该保存您的文件,并按需要单独复制它。

copy

代码语言:javascript
复制
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 .');
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56361313

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档