RuntimeException SplFileInfo::getSize(): stat failed for... Laravel 4 上传图片

2022-01-08 00:00:00 file-upload php spl laravel-4 splfileobject

我使用 laravel 4(上次更新),我创建了一个 表单,我们可以在其中上传图片(徽标/头像).我在 MAC OS, 我使用 sublime Text 3、laravel 和 MAMP 应用程序.我所有的配置都设置正确,没有运行问题.

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.

我的问题是,当我从表单提交到达字段时,我遇到了这个 错误:RuntimeExceptionSplFileInfo::getSize():/Applications/MAMP/tmp/php/phpRUJfMX

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

这是我表单中的代码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',
  );

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

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

但是当我提交表单时,我有这个错误:RuntimeExceptionSplFileInfo::getSize():/Applications/MAMP/tmp/php/phpRUJfMX 统计失败

But When I submit the form, I have this error: 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 - 提供有关文件的信息),也就是说,需要从上传的文件中获取信息,这里是大小,但我在我的控制器中尝试这个,就在 where 行之前这是 $uploaded 我将文件移动到所选文件夹中的位置:

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

但是,当我这样做时,我遇到了另一个错误:验证器没有将文件识别为图像** 并要求我上传有效格式.我想我需要纠正我遇到的第一个错误 (SplFileInfo::getSize())

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.

推荐答案

短版:如果文件大小大于 php.ini 的 upload_max_filesize,Laravel 的 Symphony 源返回文件大小为 0.

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

建议检查您的 php.ini 文件.我不确定 MAMP 如何处理 php.ini,但如果你正在运行 Laravel,它就在那里.在 php.ini 中,upload_max_filesize 可能小于您尝试上传的文件.该值恰好在我的 Ubuntu 12.10 VM 上默认为 2megs.

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.

检查

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

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

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) 

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

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.

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

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

相关文章