【问题标题】:Can't upload some filetypes无法上传某些文件类型
【发布时间】:2015-06-24 15:59:04
【问题描述】:

我正在使用 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() !}}

我的表单请求

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)。

【问题讨论】:

    标签: laravel laravel-5


    【解决方案1】:

    尝试使用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);
    

    }

    【讨论】:

    • 好吧,让我们试试看,你能告诉我为什么 png、jpeg、git 有效,但 docs、pdf 或 txt 无效。有什么做错了吗。谢谢你
    • 你使用 \Image::make 保存文档、pdf、txt 这可能是问题。
    猜你喜欢
    • 2012-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多