laravel Laravel4:上传照片

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18352811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:18:34  来源:igfitidea点击:

Laravel4: upload a photo

phpformslaravellaravel-4photo-gallery

提问by FrancescoMussi

I'm stuck.. I want to add to give the possibity to add a new photo through a form. I have the Photo resource properly set up. The table photo with: id, title, caption, path:string.

我被卡住了.. 我想添加以提供通过表单添加新照片的可能性。我已经正确设置了照片资源。表格照片:id、title、caption、path:string。

This is the form that i made until now in the view photo.create:

这是我迄今为止在视图 photo.create 中制作的表格:

{{ Form::open(array('route' => 'photos.store'),'image/save', array('files'=> true)) }}

 {{ Form::label('title', 'Title: ') }}
 {{ Form::text('title') }}

 {{ Form::label('caption', 'Caption: ') }}
 {{ Form::textarea('caption') }} <br>

 {{ Form::submit('Add Photo', array('class' => 'btn btn-primary' )) }}

 {{ Form::close() }}

How can i add a button thanks to which select a new file?

由于选择新文件,我如何添加按钮?

Thank you!!

谢谢!!

EDIT:

编辑:

This is the simple store method i made until now:

这是我到目前为止所做的简单存储方法:

public function store()
{
    $input = Input::all();

    $rules = array('title' => 'required', 'path' => 'required');

    $validation = Validator::make($input, $rules);

    if ($validation->passes()) 
    {

        $photo = new Photo();
        $photo->title = $input['title'];
        $photo->caption = $input['caption'];
        $photo->path = $input['path'];

        $photo->save();

        return Redirect::route('photos.index');
    }

    return Redirect::back()->withInput()->withErrors($validation)->with('message','There were validation message');

}

How can i put correctly there that implementation to retrieve the file and store in the folder located in public/img ? And then how can i save that path and put into $photo->path ?

我如何正确放置该实现以检索文件并将其存储在位于 public/img 的文件夹中?然后我如何保存该路径并放入 $photo->path ?

Thank you very much!!

非常感谢!!

回答by netvision73

Use Form::file('image')

Form::file('image')

Then, you can retrieve your uploaded file with Input::file('image')and move it to a destination with Input::file('file')->move(YOUR_DESTINATION_PATH);

然后,您可以使用 检索上传的文件Input::file('image')并将其移动到目的地Input::file('file')->move(YOUR_DESTINATION_PATH);

References : http://laravel.com/docs/requests#filesand http://laravel.com/docs/html#file-input

参考资料:http: //laravel.com/docs/requests#fileshttp://laravel.com/docs/html#file-input



edit :

编辑 :

To store your uploaded file to public/img: Input::file('file')->move(base_path() . '/public/img');

要将上传的文件存储到public/imgInput::file('file')->move(base_path() . '/public/img');

Storing path in database :

在数据库中的存储路径:

$photo->path = base_path() . '/public/img' . Input::file('file')->getClientOriginalName(); // if you want your real path on harddrive

or

或者

$photo->path = URL::to('img/' . Input::file('file')->getClientOriginalName()); // if you want an exploitable path for http render


second editSee my correction on your form http://paste.laravel.com/KX9

第二次编辑在你的表格上看到我的更正http://paste.laravel.com/KX9

@extends('master')
@section('blog')
<div class="span12 well">
    {{ link_to_route('photos.index', 'Back to index') }}
</div>

<div class="span12 well">
   {{ Form::open(array('route' => 'photos.store', 'files'=> true)) }}
     {{ Form::label('title', 'Title: ') }}
     {{ Form::text('title') }}
     {{ Form::label('caption', 'Caption: ') }}
     {{ Form::textarea('caption') }} 
     {{ Form::label('image', 'Image: ') }}
     {{ Form::file('image') }}
     <br>
     {{ Form::submit('Add Photo', array('class' => 'btn btn-primary' )) }}
   {{ Form::close() }}
   <br><br>
   @if($errors->any())
      {{ implode('', $errors->all('<li class="error">:message</li>')) }}
   @endif
</div>
@stop

回答by Rob Gordijn

you can use Form::file('image');

您可以使用 Form::file('image');

see http://laravel.com/docs/html#file-input

http://laravel.com/docs/html#file-input

回答by Himanshu Dwivedi

{{ Form::open(array('action' => 'HomeController@upload', 'files'=>true)) }}
{{ Form::label('image', 'Upload Image')}}
{{ Form::file('image') }}
{{ Form::submit('Submit') }}
{{ Form::close() }}

Step2:

第2步:

public function upload(){
    $image = Input::file('image');
    $savepath = 'public/uploads/';
    $filename = $image->getClientOriginalName();
    Input::file('image')->move($savepath, $filename);
}

Final Step:

最后一步:

Route::get('/upload' function()    
{
    return View::make('upload');    
});    
Route::post('/upload', 'HomeController@upload');