【发布时间】:2014-07-31 01:12:52
【问题描述】:
我使用 laravel 4(上次更新),我创建了一个 表单,我们可以在其中上传图片(徽标/头像)。我在 MAC OS, 我使用 sublime Text 3、laravel 和 MAMP 应用程序。我所有的配置都设置正确,没有运行问题。
我的问题是,当我从表单提交到达字段时,我遇到了这个 错误:RuntimeException SplFileInfo::getSize(): /Applications/MAMP/tmp/php/phpRUJfMX
这是我表单中的代码nameOfTheForm.blade.php:
@extends('layout.default')
@section('title')
Name Of My Project - EditProfile
@stop
@section('content')
{{Form::open(array('url'=>'uploadAvatar','files' => true))}}
<p>
{{Form::label('pseudo','pseudo (*): ')}}
{{Form::text('pseudo',Input::old('nom'))}}
</p>
@if ($errors->has('pseudo'))
<p class='error'> {{ $errors->first('pseudo')}}</p>
@endif
<br>
<br>
<p>
{{Form::label('url_Avatar','Avatar: ')}}
{{Form::file('url_Avatar',Input::old('Url_Avatar'))}}
</p>
@if ($errors->has('url_Avatar'))
<p class='error'> {{ $errors->first('url_Avatar')}}</p>
@endif
<br>
<br>
<p>
{{Form::submit('Validate your avatar')}}
</p>
{{Form::close()}}
@stop
这是来自我的控制器的代码:
public function uploadAvatar() {
//*****UPLOAD FILE (on server it's an image, on the DB it's an url*****
$file = Input::File('url_Avatar');
//set a register path to the uploaded file
$destinationPath = public_path().'/upload/';
//have client extension loaded file and set a random name to the uploaded file, produce a random string of length 32 made up of alphanumeric characters [a-zA-z0-9]
$filename = $destinationPath . '' . str_random(32) . '.' . $file->getClientOriginalExtension();
$uploaded = Input::File('url_Avatar')->move($destinationPath,$filename);
//*****VALIDATORS INPUTS and RULES*****
$inputs = Input::all();
$rules = array(
'pseudo' => 'required|between:1,64|unique:profile,pseudo',
//urlAvatar is an url in database but we register as an image on the server
'url_Avatar' => 'required|image|min:1',
);
上传的文件代码完美运行,我将文件注册到我想要的所选文件夹中。我的路线没有问题(无需显示这部分代码)。
但是当我提交表单时,我有这个错误: RuntimeException SplFileInfo::getSize(): /Applications/MAMP/tmp/php/phpRUJfMX 统计失败
错误信息详情: 打开:/Applications/MAMP/htdocs/nameOfMyProject/vendor/laravel/framework/src/Illuminate/Validation/Validator.php
}
elseif (is_array($value))
{
return count($value);
}
elseif ($value instanceof File)
{
return $value->getSize() / 1024;
}
else
看来,Laravel 需要(stat - 提供有关文件的信息),也就是说,需要从上传的文件中获取信息,这里是大小,但我在控制器中尝试了这个,就在 where 行之前这是 $uploaded 我将文件移动到所选文件夹中的位置:
//I add this line code before
$size= $file->getSize();
$uploaded=Input::File('url_Avatar')->move($destinationPath,$filename);
但是,当我这样做时,我遇到了另一个错误:验证器没有将文件识别为图像** 并要求我上传有效格式。我想我需要纠正我遇到的第一个错误 (SplFileInfo::getSize())
如果您有任何想法...谢谢。
【问题讨论】:
-
您必须在移动文件之前读取文件的属性。看看stackoverflow.com/questions/53114252/…
标签: php file-upload laravel-4 spl splfileobject