Laravel上传多张图片,所有具有相同名称且未移动文件的记录

时间:2019-07-23 08:57:16

标签: php laravel laravel-5

我是Laravel的新手,我开发了一个网络应用程序,用户可以在其中上传汽车广告,其他用户可以在下面发表评论。我没有开发一个简单的CRUD应用程序,而是考虑开发一个与这些广告相关并存储照片的表格。至此,我已经建立了关系,可以显示图像,但有两个问题:第一个问题是并非所有图像都已移至storage / photos文件夹,第二个问题是数据库中的所有记录都具有一样的名字。例如,如果我要上传图像1,2,3,则在数据库中所有文件的名称都将为1。

亲爱的,您可能会找到商店代码。此代码存储所有广告以及图像(如果存在)。

public function store(Request $request)
{
    //
    $this->validate($request, [
        'name' => 'required',
        'cc' => 'required',
        'desc' => 'required',
        'date' => 'required'
    ]);
    $ad = new Ad;

    $ad->name = $request->input('name');
    $ad->cc = $request->input('cc');
    $ad->desc = $request->input('desc');
    $ad->date = $request->input('date');
    $ad->user_id = auth()->user()->id;
    $ad->save();

    if ($request->hasFile('photos')) {
        $allowedfileExtension = ['pdf', 'jpg', 'png', 'docx'];
        $files = $request->file('photos');
        foreach ($files as $file) {
            $filename = $file->getClientOriginalName();
            $extension = $file->getClientOriginalExtension();
            //$file->move('storage/photos', $filename); 
            $check = in_array($extension, $allowedfileExtension);

            if ($check) { 
                foreach ($request->photos as $photo) {
                    $photo->move('storage/photos', $filename); 
                    $img = new AdsPhoto;
                    $img->ad_id = $ad->id;
                    $img->filename = $filename;
                    $img->save();
                }
                return redirect('/home')->with('success', 'ad + image created');
            } else {
                echo '<div class="alert alert-warning"><strong>Warning!</strong> Sorry Only Upload png , jpg , doc</div>';
            }
        }
    }
    return redirect('/home')->with('success','ad created');
}

提前谢谢!

2 个答案:

答案 0 :(得分:0)

第一个错误,您确实循环了两次

第二个错误(您可能没有意识到),您没有合并文件名和扩展名,这将在您检索刀片中的图像时引起问题

因此需要对代码进行更正并加上注释以对其进行解释

if ($request->hasFile('photos')) {
    $allowedfileExtension = ['pdf', 'jpg', 'png', 'docx'];
    $files = $request->file('photos');
    foreach ($files as $file) {
        $filename = $file->getClientOriginalName();
        $extension = $file->getClientOriginalExtension();
        //$file->move('storage/photos', $filename); 
        $check = in_array($extension, $allowedfileExtension);
        $fullpath = $filename . '.' . $extension ; // adding full path

        if ($check) { 
            // removing 2nd loop
           $file->move('storage/photos', $fullpath); // you should include extension here for retrieving in blade later
           $img = new AdsPhoto;
           $img->ad_id = $ad->id;
           $img->filename = $filename;
           $img->save();
        }

        } else {
            echo '<div class="alert alert-warning"><strong>Warning!</strong> Sorry Only Upload png , jpg , doc</div>';
        }
    }
    return redirect('/home')->with('success', 'ad + image created');
}

答案 1 :(得分:0)

从文档中,如果要将文件存储而不是公用文件夹中,则需要将以下几行代码放在控制器类中

<?php
...
use Illuminate\Support\Facades\Storage;
...
class YourController extends Controller{
...
// Automatically generate a unique ID for file name...
Storage::putFile('photos', new File('/path/to/photo'));
...
}

此命令会将您的文件放入存储文件夹