【问题标题】:Laravel 4: moving files once uploadedLaravel 4:上传后移动文件
【发布时间】:2014-12-28 08:27:15
【问题描述】:

我在尝试在我的应用上上传照片和任何类型的文件时遇到问题,因为确实上传但作为 .tmp 文件,它们无法在我的视图中正确显示

1.-我的表单,我正在尝试上传包含姓名、群组、电子邮件、描述和照片的成员

  {{Form::open(array('action' => 'AdminController@addMember','files'=>true)) }}
  {{ Form::label('file','Agregar Imagen',array('id'=>'','class'=>'')) }}
  {{ Form::file('file','',array('id'=>'','class'=>'')) }}
  <br/>
  {{Form::text('name','',array('class' => 'form-control','placeholder'=> 'Nombre'))}}
  {{Form::text('group','',array('class' => 'form-control','placeholder'=> 'Cargo'))}}
  {{Form::text('email','',array('class' => 'form-control','placeholder'=> 'Correo'))}}  
  {{Form::textarea('description','',array('class' => 'form-control','placeholder'=>''))}}
  <!-- submit buttons -->
  {{ Form::submit('Guardar') }}          
  <!-- reset buttons -->
  {{ Form::reset('Reset') }}          
  {{ Form::close() }}

2.-我在控制器中的上传功能

public function addMember()
{
    $name = Input::file('file')->getClientOriginalName();
    $newname = Input::file('file')->getFilename();    
    Input::file('file')->move(storage_path(),$name);
    $subpath = storage_path();
    $path = $subpath.'/'.$newname2;
    $name2 = Input::get('name');
    $email = Input::get('email');
    $description = Input::get('description');
    $group = Input::get('group');

    DB::table('contactgroups')->insert(
        array('group' => $group, 'name' => $name2, 'path' => $path, 'email' => $email, 'description' => $description)
    );
    $members = DB::table('contactgroups')->get();
    return View::make('admin.members',['members' => $members]);
} 

我知道我应该使用模型将内容上传到我的数据库中,但这不是现在的问题

3.- 我的显示视图

@extends('layouts.main')
@section('content')
    @foreach($members as $member)   
        <div class = "row fondue">
                <h3><div class="col-md-12"><b><?=$member->name ?></b></div></h3>    
                <div class="col-md-4"> <img src="<?=$member->path ?>" alt="Image" class = "contact-img"></div>
                <div class="col-md-4"><?=$member->description ?></div>
                <div class="col-md-4"><?=$member->email ?></div>

        </div>  

    @endforeach

@stop

仅此而已...信息已保存在数据库中,但图像在视图中未正确显示,并且文件以 tmp 文件的形式上传,我不知道为什么

【问题讨论】:

  • storage 目录不可(或不应)公开访问。您必须将图像存储在 public 的某个位置
  • 但我有一些私人文件我不想公开
  • 你的意思是只有登录用户才能访问它?
  • 不完全是,有些文档是供公众访问的,有些文档应该只对管理员可见
  • 好吧,这是可能的,但如果您删除此问题并按照“我如何使用storage 目录中的(受保护的)资产”的方式提出一个新问题,我会更喜欢它。在问题中,您还将指定如何验证用户(您是否已经为此设置了路由过滤器?)谢谢。

标签: php laravel laravel-4


【解决方案1】:

来自 Laravel 文档

移动上传的文件

Input::file('photo')->move($destinationPath);

Input::file('photo')->move($destinationPath, $fileName);

来源:http://laravel.com/docs/4.2/requests#files

【讨论】:

    猜你喜欢
    • 2016-10-08
    • 2013-07-22
    • 2013-04-06
    • 2015-03-21
    • 2016-03-29
    • 2013-08-11
    • 2014-03-07
    • 2017-09-03
    • 1970-01-01
    相关资源
    最近更新 更多