【问题标题】:Laravel 5 Form upload direct to S3 Bucket not workingLaravel 5 Form直接上传到S3 Bucket不起作用
【发布时间】:2015-03-05 14:25:17
【问题描述】:

我已经用 Laravel 5 玩了大约一个星期并且喜欢它,但我这辈子都无法让它工作。 我有一个简单的页面,可以将图像上传到我的 S3 存储桶。

{!! Form::open(['post' => '/dash/profile/avatar/', 'files' => true]) !!}
        <div class="form-group">
            {!! Form::file('avatar') !!}
        </div>
        <div class="form-group">
            {!! Form::submit('Upload', ['class' => 'form-control button']) !!}
        </div>
{!! Form::close() !!}

适用路线

Route::get('/dash/profile/avatar', 'ProfileController@updateAvatar');

Route::post('/dash/profile/avatar/', 'ProfileController@uploadAvatar');

还有我的 ProfileController 上传方法

public function uploadAvatar(Filesystem $filesystem, Request $request)
    {
        if (Auth::check())
        {
            $filename = $request->file('avatar')->getFilename();
            $file = $request->file('avatar');
            $filesystem->put('test.jpg', $file);
            return redirect('dash/profile')->with(['flash_message' => 'Your Avatar has been successfully updated!']);
        } else {
            return redirect('auth/login');
        }
    }

我在 S3 存储桶中得到的只是一个名为“test.jpg”的文件,正如预期的那样,但它只有几个字节,而不是我尝试上传的 ~500KB 图像。 谁能告诉我为什么? 提前致谢! 克里斯

【问题讨论】:

    标签: php forms laravel amazon-s3 laravel-5


    【解决方案1】:

    你可能想试试这个:

    $filesystem->put('test.jpg', \File::get($file->getRealPath());
    

    put 的第二个参数是将写入文件的内容,因此请确保传递实际内容而不是文件对象。

    【讨论】:

    • 虽然这段代码 sn-p 可以解决问题,including an explanation 确实有助于提高您的帖子质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
    • 也可以在put的第二个参数中使用PHP自带的file_get_contents()函数来代替\File::get(),所以$filesystem-&gt;put('test.jpg', file_get_contents('path/to/test.jpg'));
    【解决方案2】:

    解决了问题!它与 Laravel 无关。将upload_max_filesizepost_max_size 更改为足够大的值(50M)。通过 sudo pkill php5-fpm 销毁所有 php5-fpm 会话,然后通过 service php5-fpm start 启动 php5-fpm 服务。然后一切正常,文件上传到/tmp

    【讨论】:

      猜你喜欢
      • 2015-04-12
      • 1970-01-01
      • 1970-01-01
      • 2019-04-05
      • 2017-07-28
      • 1970-01-01
      • 1970-01-01
      • 2011-09-01
      • 2012-03-14
      相关资源
      最近更新 更多