RuntimeException SplFileInfo::getSize(): stat failed for... Laravel 4 upload image
Asked Answered
M

4

8

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.

Manger answered 10/6, 2014 at 10:4 Comment(1)
You must read the file's attributes before move it. Take a look at #53114752Trichina
S
5

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
Submultiple answered 16/7, 2014 at 22:5 Comment(3)
I don't have my code with me for now, but tomorrow I will try this solution. Thank you to both of you for your understanding.Manger
and so it works, MAMP has another php.ini file in htdocs folder and I can change some parameters like image size file. So I follow your steps, with your code.Manger
php.net/manual/en/ini.core.php#ini.post-max-size - to follow up on Ryan's commentNorland
H
6

I know this question has been answered quite some time ago, but I found something on the PHP website which might be useful because it actually explains why the error occurs:

If you're using Symfony's UploadedFile, please be aware that if you call this method after you call @move, you will most likely get some obscenely untraceable error, that says:

stat failed

Which if you really think about it, it does makes sense, the file has been moved by Symfony, but getSize is in SplFileInfo, and SplFileInfo doesn't know that the file has been moved.

Source: https://www.php.net/manual/en/splfileinfo.getsize.php#122780

Heinous answered 28/12, 2021 at 11:13 Comment(0)
S
5

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
Submultiple answered 16/7, 2014 at 22:5 Comment(3)
I don't have my code with me for now, but tomorrow I will try this solution. Thank you to both of you for your understanding.Manger
and so it works, MAMP has another php.ini file in htdocs folder and I can change some parameters like image size file. So I follow your steps, with your code.Manger
php.net/manual/en/ini.core.php#ini.post-max-size - to follow up on Ryan's commentNorland
T
3

I got same error In Laravel 6x. This error is due to null value for column which was not nullable.

To resolve I just changed column to accept null after that is working fine.

Territoriality answered 21/9, 2019 at 9:33 Comment(0)
P
0

i solve my problem with edit columns attribute default "null" and the problem gone.

Platitudinize answered 4/10, 2019 at 5:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.