2015-06-08 67 views
3

我一直有我的圖片上傳輸入問題。我試圖在我的Laravel 5項目中創建一個文件上傳輸入,但我遇到了保存到數據庫映像表中的路徑問題。Laravel 5圖片上傳錯誤的數據庫路徑

然而,當數據庫保存輸入圖像的路徑時,表單正在工作並正在發佈:/Applications/MAMP/tmp/php/phptvwTYW而不是僅使用文件名。

此外,該文件正在被移動到正確的public/img文件夾。

代碼

public function store(PostRequest $request) 
{ 
    $this->createPost($request); 

    $destinationpath = public_path() . '/img/'; 

    $filename = $request->file('image_url')->getClientOriginalName(); 

    $request->file('image_url')->move($destinationpath,$filename); 

    flash()->success('Your Post Has Been Created!'); 

    return redirect('posts'); 
} 

回答

-1

它,因爲你移動之前保存,

//before moving 
$request->file('image_url')->getPath(); // returns Applications/MAMP/tmp/php/php... 

有你可以做到這一點

//After moving 
$res = $request->file('image_url')->move($destinationpath,$filename); 
$my_new_path = $res->getPath(); // returns [public_path]/img/filename.jpg 

您將新移動的文件的完整路徑可以通過在移動文件後更新您的文章或使用事件來保存它節能 的時候把它http://laravel.com/docs/master/eloquent#events

+0

這裏http://stackoverflow.com/questions/25993123/laravel-4-2-storing-image-path-to-database –

+1

但這使得2個查詢,創建和更新 – cnu

2

這裏是目前使用在我的項目

public function postCreateInternal(CreateDocumentInternalRequest $request) { 
     $data_information = $request->only(['title', 'assigned_to', 'folder_id', 'document_source']); 
     if ($request->hasFile('file_name') && $request->file('file_name')->isValid()) { 
      $document = $request->file('file_name'); 
      #creating file Name 
      $mytime = Carbon::now(); 
      $date_now = $mytime->toDateTimeString(); 
      $date_now = $this->prepareFileNameString($date_now); 
      $document_extension = $document->getClientOriginalExtension(); 

      $document_name = $this->prepareFileNameString(basename($document->getClientOriginalName(), '.' . $document_extension)); 
      $document_fullName = $document_name . "_" . ($date_now) . "." . $document_extension; 
      $data_information['file_type'] = $document->getMimeType(); 
      $data_information['file_size'] = $document->getSize(); 
      $data_information['file_name'] = $document_fullName; 
      $data_information['file_download_type'] = "Internal"; 
      $document->move(public_path() . '/uploads/documents/', $document_fullName); 
     } 
     if ($pot = $this->document->create($data_information)) { 
      $this->notification_admin->sendCreateDocument($data_information); 
      return redirect()->route('documents')->with('success', trans('document.create.msg_success')); 
//   return redirect()->route('update/document', $pot->id)->with('success', trans('document.create.msg_success')); 
     } 
     return redirect()->route('create/document')->with('error', trans('document.msg_error')); 
    } 

CreateDocumentInternalRequest按Laravel 5

,並查看文件基本上使用的文件和其他數據驗證樣本控制器功能看這裏似乎喜歡:

{!! Form::open(["class"=>"form-horizontal","data-parsley-validate"=>"data-parsley-validate",'role'=>'form','files'=>true]) !!} 
<div class="form-group required {{ $errors->first('file_name', ' has-error') }}"> 
    {!!Form::label('file_name', trans('document.label.file_name'), array('class' => 'col-md-4 control-label left-label'))!!} 
    <div class="col-sm-6"> 
     {!! Form::file('file_name') !!} 
     {!! $errors->first('file_name', '<span class="help-block">:message</span>') !!} 
    </div> 
</div> 
{!! Form::close() !!} 

在我目前的實施,冷杉我正在檢查上傳的文件,用當前時間戳重命名文件名並重新上傳我的願望位置。 如果您需要任何幫助,我提供的方法讓我知道以更好的方式改善。

0

試試這個:

public function store(PostRequest $request, Post $post) { 
    $destinationpath = public_path() . '/img/'; 

    $filename = $request->file('image_url')->getClientOriginalName(); 

    $request->file('image_url')->move($destinationpath,$filename); 

    $post->create([ 
     'field1' => $val1, 
     'imageField' => $filename, 
     'field2' => $val2 
     ]); 

    flash()->success('Your Post Has Been Created!'); 

    return redirect('posts'); 
} 
0

我找到了解決我的問題。 我必須對我的控制器中的商店和createPost函數進行一些更改才能完成此工作。

對於控制器我有:

 
public function store(PostRequest $request) 
{ 
     $destinationpath = public_path() . '/img/'; 

     $filename = $request->file('image_url')->getClientOriginalName(); 

     $request->file('image_url')->move($destinationpath,$filename); 

     $this->createPost($request, $filename); 

     flash()->success('Your Post Has Been Created!'); 

     return redirect('posts'); 
} 


private function createPost(PostRequest $request, $new_url) 
{ 
     $post = Auth::user()->posts()->create($request->all()); 

     $post->image_url = $new_url; 

     $post->save(); 

     $this->syncTags($post, $request->input('tag_list')); 

     return $post; 
} 

我希望這可以幫助其他人可能正在運行到同樣的問題。 謝謝大家的幫助!

1

這是非常簡單的方法:

public function store(PostRequest $request) 
{ 
    $image_name = $request->file('image')->getClientOriginalName(); 
    $request->file('image')->move(base_path().'/public/images', $image_name); 
    $post = ($request->except(['image'])); 
    $post['image'] = $image_name; 
    Post::create($post); 
    Session::flash('success_message', 'Post has been added successfully!'); 
    return redirect('teacher'); 
} 
1
public function profileUpdate($id) 
    { 

    if(!Entrust::can('profile_update_employee')) 
     return Redirect::to('/dashboard')->withErrors(Config::get('constants.NA')); 

    if(!Helper::getMode()) 
     return Redirect::back()->withErrors(Config::get('constants.DISABLE_MESSAGE')); 

    $employee = User::find($id); 

    if(!$employee) 
     return Redirect::to('employee')->withErrors(Config::get('constants.INVALID_LINK')); 

    $rules = array(
     'photo' => 'image|image_size:<=2000|max:100000', 
     'date_of_birth' => 'date', 
     'date_of_joining' => 'date', 
     'date_of_leaving' => 'date', 
     'employee_code' => 'required|unique:profile,employee_code,'.$employee->Profile->id.',id' 
    ); 


    $validator = Validator::make(Input::all(), $rules); 

    if ($validator->fails()) 
     return Redirect::to('/employee/'.$id."#basic")->withErrors($validator); 

    Activity::log('Profile updated'); 
    $profile = $employee->Profile ?: new Profile; 
    $photo = $profile->photo; 
    $data = Input::all(); 
    $profile->fill($data); 
    if(Input::get('date_of_birth') == '') 
     $profile->date_of_birth = null; 
    if(Input::get('date_of_joining') == '') 
     $profile->date_of_joining = null; 
    if(Input::get('date_of_leaving') == '') 
     $profile->date_of_leaving = null; 

    if (Input::hasFile('photo') && Input::get('remove_photo') != 1) { 


     $filename = Input::file('photo')->getClientOriginalName(); 
     $extension = Input::file('photo')->getClientOriginalExtension(); 
     $file = Input::file('photo')->move('assets/user/', $employee->username.".".$extension); 

     DB::insert('insert into ez_profile (id, photo) values ($id, $photo)'); 

     $img = Image::make('assets/user/'.$user->username.".".$extension); 
     $img->resize(200, null, function ($constraint) { 
     $constraint->aspectRatio(); 
     }); 
     $img->save('assets/user/'.$user->username.".".$extension); 
     $profile->photo = $employee->username.".".$extension; 
     } elseif(Input::get('remove_photo') == 1){ 
     File::delete('assets/user/'.$profile->photo); 
     $profile->photo = null; 
     } 
     else 
     $profile->photo = $photo; 
     $employee->profile()->save($profile); 
     return Redirect::to('/employee/'.$id.'/#basic')->withSuccess(Config::get('constants.SAVED')); 
     }