我使用laravel 4 (last update),我创建了一个表单,我们可以在其中上传图像(徽标/头像)。我在MAC OS 上,我使用sublime Text 3、laravel和MAMP Applications。我所有的配置都设置正确,没有运行问题。
我的问题是,当我从表单提交到达字段时出现此错误: RuntimeException SplFileInfo::getSize(): stat failed for /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(): stat failed for /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 - 提供有关文件的信息),也就是说,需要从上传的文件中获取信息,这里是大小,但我在我的控制器中尝试这个,就在 $uploaded 的行之前我将文件移动到我选择的文件夹中的位置:
//I add this line code before
$size= $file->getSize();
$uploaded=Input::File('url_Avatar')->move($destinationPath,$filename);
但是,当我这样做时,我遇到了另一个错误:验证器不会将文件识别为图像** 并要求我上传有效格式。我想我需要纠正我遇到的第一个错误(SplFileInfo::getSize())
如果您有任何想法...谢谢。