RuntimeException SplFileInfo :: getSize():stat失败... Laravel 4上传图片 [英] RuntimeException SplFileInfo::getSize(): stat failed for... Laravel 4 upload image

查看:1710
本文介绍了RuntimeException SplFileInfo :: getSize():stat失败... Laravel 4上传图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 laravel 4(最近更新),创建表单,我们可以在上传图片(徽标/头像)。我在 MAC OS 上使用崇高文本3 laravel MAMP应用。我的问题是,当我从表单提交到达字段时,我有这个错误 RuntimeException
SplFileInfo :: getSize():stat失败/ Applications / MAMP / tmp / php / phpRUJfMX

$ b 这里是代码从我的表单 nameOfTheForm.blade.php

  @extends('layout.default') 
@section('title')
我的项目名称 - 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>
$ b $ {{Form :: close()}}
@stop

以下是控制器中的代码:

  public function uploadAvatar(){


// *****上传文件(在服务器上它是一个图像,在数据库上它是一个url *****
$ file = Input :: File('url_Avatar ');

//设置上传文件的路径
$ destinationPath = public_path()。'/ upload /';

//有客户端扩展名加载文件,并为上传的文件设置一个随机名,产生一个由字母数字字符[a-zA-z0-9]

$ filename = $ destinationPath组成的长度为32的随机字符串。 str_random(32)。'。'。$ file-> getClientOriginalExtension();

$ uploaded = Input :: File('url_Avatar') - > move($ destinationPath,$ filename) ;

// ***** VALIDATORS INPUTS和RULES *****
$ inputs = Input :: all();
$ rules = array(
'pseudo'=>'required | between:1,64 | unique:profi le,pseudo',
// urlAvatar是数据库中的一个url,但是我们在服务器上注册为图片
'url_Avatar'=> 'required | image | min:1',
);

上传的文件代码完美,我将文件注册到所需的文件夹中。我没有问题,我的路线(不需要显示这部分的代码)。

但是当我提交表单,我有这个错误:
RuntimeException
SplFileInfo :: getSize():stat失败/ Applications / MAMP / tmp / php / phpRUJfMX


$ b

错误信息详情:
open:/ Applications / MAMP / htdocs / nameOfMyProject / vendor / laravel / framework / src /Illuminate/Validation/Validator.php

 } 
elseif(is_array($值))
{
return count($ value);

elseif($ value instanceof File)
{
return $ value-> getSize()/ 1024;






$ b

看来,Laravel需要(stat - 给出信息关于一个文件),也就是说,需要从上传的文件中获得信息,这里是大小,但是我在我的控制器中试着在$上传的行之前,在我选定的文件夹中移动文件: / p>

  //我在
$ size = $ file-> getSize()之前添加这行代码。
$ uploaded = Input :: File('url_Avatar') - > move($ destinationPath,$ filename);

但是,当我这样做,我有另一个错误:验证程序不r ** ecognize这些文件作为图像**请求我上传有效格式。我想我需要纠正我有(SplFileInfo :: getSize())的第一个错误



如果您有任何想法...谢谢如果文件大小大于php.ini的<$,则Laravel的Symphony源文件返回的文件大小为0。 C $ C>的upload_max_filesize 。建议检查你的php.ini文件。

我不确定MAMP如何处理php.ini,但是如果你正在运行Laravel,它就在那里。在php.ini中,upload_max_filesize可能比您尝试上传的文件小。这个值恰好在我的Ubuntu 12.10虚拟机上默认为2megs。

检查
$ b $ / vendor / symphony / http-foundation / Symphony / Component / HttpFoundation / File / UploadedFile.php



并检查 getMaxFilesize()。默认情况下,此方法从主机系统上的PHP.ini文件中获取 upload_max_filesize 值。



有趣的是,如果你做

$ p $ foo = Input :: file('uploaded_file')
var_dump($ foo)



使用Laravel的 die& dump dd($ foo),然后一个大于php.ini的upload_max_filesize的文件返回大小为0。

除了upload_max_filesize之外,PHP文档还提到检查php.ini,以便:

   -  post_max_size必须大于upload_max_filesize 
- memory_limit应该大于post_max_size


I work with laravel 4 (last update), I create a form where we could upload an image (logo/avatar). I am on MAC OS, I use sublime Text 3, laravel and MAMP Applications. All my configuration is setting right, no running problems.

My probleme is that I have this error when I submit the reaching fields from my form: RuntimeException SplFileInfo::getSize(): stat failed for /Applications/MAMP/tmp/php/phpRUJfMX

Here's the code from my form 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

Here's the code from my controller:

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',
  );

The uploaded file code works perfect, I register the file in the selected folder I want. I Have no problem with my routes (no need to show this part of the code).

But When I submit the form, I have this error: RuntimeException SplFileInfo::getSize(): stat failed for /Applications/MAMP/tmp/php/phpRUJfMX

error info details: open:/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

It seems, that Laravel needs (stat - Gives information about a file), that is to say, needs to have the informations from the uploaded file, here the size, but I try this in my controller just before the line where here's $uploaded where I move the file in my selected folder:

//I add this line code before
$size= $file->getSize();
$uploaded=Input::File('url_Avatar')->move($destinationPath,$filename);

But, when I did that, I have another error: the validator doesn't r**ecognize the files as an image** et ask me to upload a valid format. I think I need to correct the first errors I had (SplFileInfo::getSize())

If you have any ideas... Thank you.

解决方案

Short version: Laravel's Symphony source returns file size of 0 if the file size is larger than php.ini's upload_max_filesize.

Recommend checking your php.ini file. I'm not sure how MAMP handles php.ini but it's there if you're running Laravel. In php.ini, the upload_max_filesize may be smaller than the file you're attempting to upload. That value happened to be defaulted on my Ubuntu 12.10 VM to 2megs.

Check

/vendor/symphony/http-foundation/Symphony/Component/HttpFoundation/File/UploadedFile.php

and check the getMaxFilesize(). By default, this method grabs the upload_max_filesize value from your PHP.ini file on your host system.

The interesting thing about this is that if you do

$foo = Input::file('uploaded_file') 
var_dump($foo) 

or

use Laravel's die&dump dd($foo), then a file that's larger than php.ini's upload_max_filesize returns a size of 0.

In addition to upload_max_filesize, PHP docs also mention checking php.ini so that:

- post_max_size must be larger than upload_max_filesize
- memory_limit should be larger than post_max_size

这篇关于RuntimeException SplFileInfo :: getSize():stat失败... Laravel 4上传图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆