无法上传某些文件类型

时间:2015-06-20 15:07:29

标签: laravel laravel-5

我使用的是Laravel 5.1 上传文件类型jpeg,gif,png正在运行,但pdf,doc,docx,txt文件类型无法正常工作。

这是我的观点

{!! Form::label('department','Department', ['class'=>'col-sm-2 control-label'] ) !!}
<div class="col-sm-10">
    {!! Form::select('department', $departmentList, null, ['class'=>'form-control']) !!}

    <span style="color: red">{{ $errors->first('department') }}</span>
</div>
<div class="form-group">

    {!! Form::label('document','Chose a file or Document', ['class'=>'col-sm-2 control-label'] ) !!}

    <div class="col-sm-10">

        {!! Form::file('file', null, ['class'=>'form-control']) !!}
        <p class="help-block">Upload your document in pdf, doc. jpg, png, tif, gif</p>
        <span style="color: red"> {{ $errors->first('file') }}</span>
    </div>
</div>
{!! Form::close() !}}

我的FormRequest

public function rules()
{
    return [
        'user_id'=>'integer',

        'department'=>'required',
        'file'=>'required|mimes:application/msword, application/vnd.openxmlformats-officedocument.wordprocessingml.document, application/pdf, jpeg, png, gif'
    ];
}

}

和我的控制器

public function update($id, RepositoryRequest $request)
    {
    $repository = Repository::findOrFail($id);
   $repository->department = $request->input('department');



    $file = \Input::file('file');
    $filename = date('Y-m-d-H:i:s').'-'.$file->getClientOriginalName();

    $path =public_path('/img/repository/').$filename ;
    \Image::make($file->getRealPath())->resize(468, 249)->save($path);

    $repository->file = 'img/repository/'.$filename;

    $repository->save();

    return redirect('/')->with('flash_message', 'You have successfully updated the repository');

这是错误:

  

Decoder.php第21行中的NotReadableException:无法从中读取图像   文件(/ tmp / phpdNQvEG)。

1 个答案:

答案 0 :(得分:2)

尝试使用http://laravel.com/docs/5.1/filesystem来存储文件。

public function UploadImage() {
$file = Request::file('filefield');
$extension = $file->getClientOriginalExtension();
Storage::disk('local')->put($file->getFilename().'.'.$extension,  File::get($file));
$entry = new Upload();
$entry->mime = $file->getClientMimeType();
$entry->original_filename = $file->getClientOriginalName();
$entry->filename = $file->getFilename().'.'.$extension;

$entry->save();

$file = Storage::disk('local')->get($entry->filename);

return (new Response($file, 200))->header('Content-Type', $entry->mime);

}