Laravel Dropzone.js有些照片无法保存到

时间:2017-07-26 14:20:03

标签: laravel dropzone.js

我使用dropzone.js允许用户将多张照片上传到数据库。在为图像上传器功能设置数据库之前,我想查看图像是否可以保存到我想要的文件夹中。但是,在成功上传3张照片后(由于在图片上传后立即显示了刻度标识,我认为图像已成功上传),其中只有2张图片存在于该文件夹中。

在以下代码中,我列出了视图(包括javascript和html代码),路由和控制器文件:

CSS - >

{{html::style('https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.1.1/min/dropzone.min.css')}}

HTML - >

{!! Form::open(array('route' => 'gallery.images.upload', 'class' => 'dropzone', 'enctype' => 'multipart/form-data','files' => true, 'id' => 'addImages', 'method' => "POST")) !!}

  {{ Form::hidden('gallery_id', $gallery->id) }}

{!! Form::close() !!}

Javascript - >

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.1.1/min/dropzone.min.js"></script>

<script type="text/javascript">
    Dropzone.options.addImages = {
        maxFilesize: 5,
        acceptedFiles: 'image/jpeg,image/png,image/gif',
        paramName: "file",
    }
</script>

路线 - &gt;

Route::prefix('gallery')->group(function(){
    Route::post('/images/upload', 'GalleryController@uploadGalleryImages')->name('gallery.images.upload');
});

控制器 - &gt;

public function uploadGalleryImages(Request $request)
{

    if ($request->hasFile('file')) {

        // Get the file from the file request
        $file = $request->file('file');

        // set my file name
        $originalName = $file->getClientOriginalName();

        $filename = uniqid() . $originalName;

        $file->move('galleries/images/', $filename);

    } 
    else{
        return redirect()->route('gallery.view');
    }
}

我一直在询问几个星期,但无法得到任何答案。我真的需要你的帮助!

Screenshot of images being successfully uploaded

Screenshot of images present in the folder

1 个答案:

答案 0 :(得分:0)

这件事发生在我身上,因为服务器在浏览图片时会覆盖一些图片,因为它们会获得相同的文件名,以避免使用这样的内容来确保您拥有唯一的文件名:

$random = mt_rand(1,1000);
$filename  = time().''.$random. '.' . $file->getClientOriginalExtension();