在laravel中同时在两个不同的文件夹中上传图像 [英] in laravel Upload image in two different folder at same time

查看:61
本文介绍了在laravel中同时在两个不同的文件夹中上传图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码仅将图像保存在一个文件夹一"中.我要上传 图像同时放在两个不同的文件夹中,现在将其保存在一个文件夹中

This Code save image in only one "folder-one". I want to upload the image at the same time in two different folders, now it saving in folder-one

示例

第一文件夹"

第二个文件夹"

<?php

namespace App\Http\Controllers\Auth;
use Illuminate\Http\Request;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Mail;
use Illuminate\Auth\Events\Registered;

class RegisterController extends Controller
{

use RegistersUsers;

protected $redirectTo = '/home';

public function __construct()
{
    $this->middleware('guest');
}

protected function validator(array $data)
{
    return Validator::make($data, [
       'photo_jpeg' => 'required|image|mimes:jpeg,png,jpg|max:2048',
    ]);
}

protected function create(array $data)
{
    $photo_jpeg= time() . '.' . $data['photo_jpeg']->getClientOriginalExtension();
    $data['photo_jpeg']->move(base_path() . 'public/folder-one', $photo_jpeg);

    return user::create([
        'photo_jpeg' => $photo_jpeg,
    ]);

}

推荐答案

对于同一个文件,您不能运行两次move(),因为顾名思义,它会移动文件,因此在第二次运行时,原始文件将不再存在.

You can't run move() twice for the same file because as it name says, it moves the file, so on the second run, the original file won't exist anymore.

您必须复制文件:

$uploadPath = public_path('folder-one/');
$file = $data['photo_jpeg'];
$photo_jpeg= time() . '.' .$file->getClientOriginalExtension();
$file->move($uploadPath,$photo_jpeg);
\File::copy($uploadPath.$photo_jpeg,public_path('folder-two/').$photo_jpeg);

这篇关于在laravel中同时在两个不同的文件夹中上传图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆