Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.2] After move of uploaded file, missing temp file error. #12350

Closed
trwilliams opened this issue Feb 17, 2016 · 75 comments
Closed

[5.2] After move of uploaded file, missing temp file error. #12350

trwilliams opened this issue Feb 17, 2016 · 75 comments

Comments

@trwilliams
Copy link

This will give an error

public function addPost(ImageAddRequest $request) { if ($request->hasFile('image')){ $path = base_path()."/public/storage/pages/"; $image = $request->file('image'); $image->move($path, $image->getClientOriginalName()); $name = $request->name; } }
any instance of request after move gives and error that the temp file can not be found.

found exactly the line that is causing issue.

laravel/framework/src/Illuminate/Http/UploadedFile.php
Line 50

If I remove instanceof static it works fine.

The function is called from laravel/framework/src/Illuminate/Http/Request.php
Line 419

@taylorotwell
Copy link
Member

Can you give me some code I can use to recreate this in a fresh Laravel application?

@vinkla
Copy link
Contributor

vinkla commented Feb 18, 2016

This could be related to #12299. I'm also still having problems with this since the >=5.2.15 release.

@GrahamCampbell
Copy link
Member

@vinkla Is it broken in 5.2.19?

@vinkla
Copy link
Contributor

vinkla commented Feb 18, 2016

@GrahamCampbell Yeah, or it has at least changed in behaviour. My tests fails in versions >=5.2.15 and it works in <=5.2.14.

@magnetion
Copy link

@GrahamCampbell @vinkla You are correct. The problem surfaced in 5.2.15. Rolling back to .14 works. We can produce this consistently across 20+ sites.

It almost appears that once you upload a file and take any action (in our case sanitize then move), it kind of bombs anything else in the request scope. For example after moving the file an then using another form variable (non file related one) it throws the error as stated.

A couple more people have reported it here:
https://laracasts.com/discuss/channels/laravel/file-sometimes-got-uploaded-successfully-sometimes-it-disappears-from-the-tmp-directory

@GrahamCampbell
Copy link
Member

Can you give me some code I can use to recreate this in a fresh Laravel application?

@trwilliams
Copy link
Author

Controller

class ImageController extends \Illuminate\Routing\Controller{
   public function addPost(ImageAddRequest $request) { 
        $path = base_path()."/public/storage/"; 
         $image = $request->file('image'); 
         $image->move($path, $image->getClientOriginalName()); 
         $name = $request->name; 
         return Redirect::to('/'.$name);
    } 
}

@trwilliams
Copy link
Author

Request

class ImageAddRequest extends FormRequest {

    public function rules()
    {
        return [
            'name' => 'required',
            'image' => 'image'
        ];
    }

    public function authorize()
    {
        return true;
    }

}

@trwilliams
Copy link
Author

Form

{!! Form::open(array('files'=> true)) !!}
      {!! Form::hidden('name', 'test') !!}
      {!! Form::file('image') !!}
      {!! Form::submit('SUBMIT') !!}
{!! Form::close() !!}

@taylorotwell
Copy link
Member

Hmm, I still can't recreate this with this code sample :|

@taylorotwell
Copy link
Member

What version of Laravel are you on?

@vinkla
Copy link
Contributor

vinkla commented Feb 18, 2016

I don't know which version @trwilliams are using but @magnetion and I have issues with >=5.2.15.

@taylorotwell
Copy link
Member

@vinkla does the code snippet above break for you on 5.2.19?

@magnetion
Copy link

@taylorotwell It started happening in 5.2.15 and was occurring yesterday in 5.2.19.

@taylorotwell
Copy link
Member

OK, can someone give me maybe a different code snippet you have I might can use to recreate @vinkla or @magnetion ?

@magnetion
Copy link

@taylorotwell After the following runs, anything that occurs after this will error with the temp file does not exist error. So after running the code below, if you try and reference $request->anyformfield it errors.

if (Request::hasFile('image')) :
    $image = Request::file('image');
    $uniqueName = Universal::getUniqueFilename('storage/product', $image->getClientOriginalName());
    Request::file('image')->move('storage/product', $uniqueName);
endif;

@taylorotwell
Copy link
Member

But, it doesn't for me. That's why I'm confused. It keeps working fine even after moving the file.

@trwilliams
Copy link
Author

It happens in any version above 5.2.14. Also it only happens when getting the file form a request object.
public function addPost(ImageAddRequest $request) {$request->...}

But it works if you get the request like this.
public function addPost() {$request = Input::all(); $request['image']}

Actually everything does go through and upload fine, but it throws the error. If you go to a different page and come back you will see the it did upload correctly, we just can't have the error.

@vinkla
Copy link
Contributor

vinkla commented Feb 18, 2016

This is taken from my test method.

factory(User::class)->create();

Storage::put('test.png', base64_decode('imagedata'));

$path = public_path('images/uploads/test.png');

$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $path);

$this->call('POST', 'users/1/images', [], [], ['image' => new UploadedFile($path, 'test.png', $mime, null, null, true)]);
$this->seeJson(['type' => 'image']);
$this->assertResponseStatus(201);
exec('rm '.public_path('images/uploads/*.png'));

This is taken from my controller method.

$image = $request->file('image');

$name = strtolower(Uuid::uuid4().'.'.$image->getClientOriginalExtension());

// This is where is fails for me.
$image->move(public_path('images/uploads'), $name); 

Let me know if there is anything else I can provide.

@taylorotwell
Copy link
Member

@vinkla are you using a FormRequest or just the normal Illuminate\Http\Request?

@taylorotwell
Copy link
Member

Also @vinkla have you looked into the core at all to see where there might be a fix needed?

@trwilliams
Copy link
Author

In first message. here are the lines that need looked at.

laravel/framework/src/Illuminate/Http/UploadedFile.php
Line 50

If I remove instanceof static it works fine.

The function is called from laravel/framework/src/Illuminate/Http/Request.php
Line 419

@vinkla
Copy link
Contributor

vinkla commented Feb 18, 2016

I'm using Illuminate\Http\Request and no I've not looked into core to find a solution. Sorry about that, I will try to look more into this issue soon.

@taylorotwell
Copy link
Member

@trwilliams so what does your full patched method look like with the instanceof static removed?

Really wish I could recreate this but can't even with a Form Request. @trwilliams is yours triggered from a unit test or web UI?

@taylorotwell
Copy link
Member

Also what version of PHP?

@trwilliams
Copy link
Author

PHP 5.6.8

public static function createFromBase(SymfonyUploadedFile $file)
    {
        return $file ? $file : new static(
            $file->getPathname(), $file->getClientOriginalName(), $file->getClientMimeType(),
            $file->getClientSize(), $file->getError()
        );
    }

@vinkla
Copy link
Contributor

vinkla commented Feb 18, 2016

I'm using PHP 7.0.3 with Laravel 5.2.19.

@magnetion
Copy link

Also happening on 5.5.9

@taylorotwell
Copy link
Member

@trwilliams that would break other things. I will have to recreate this first I guess before proceeding. I've still not been able to.

@taylorotwell
Copy link
Member

You don't have cached compiled.php files with old versions of things do you?

@vinkla
Copy link
Contributor

vinkla commented Feb 18, 2016

@taylorotwell didn't work for me 😞 still works on 5.2.14 though.

@taylorotwell
Copy link
Member

OK. ill tag this in a bit.

@taylorotwell
Copy link
Member

@vinkla really? so your unit test is still broken? you dont have a compiled file in the way do you?

@vinkla
Copy link
Contributor

vinkla commented Feb 18, 2016

@taylorotwell no, I removed it before I tried to run the unit tests again.

@taylorotwell
Copy link
Member

OK Ill try to recreate your unit test problem.

@vinkla
Copy link
Contributor

vinkla commented Feb 18, 2016

Cool, let me know if there is anything else I can provide.

@taylorotwell
Copy link
Member

@vinkla what is the exact error message you receive from your test?

@taylorotwell
Copy link
Member

@vinkla I think I know your issue. See if passing an instance of Illuminate\Http\UploadedFile fixes your problem.

@vinkla
Copy link
Contributor

vinkla commented Feb 18, 2016

@taylorotwell okay, great. I will try it first thing tomorrow morning.

@vinkla
Copy link
Contributor

vinkla commented Feb 19, 2016

@taylorotwell with Illuminate\Http\UploadedFile it works. Thanks!

@CasperLaiTW
Copy link
Contributor

@vinkla You should create Illuminate\Http\UploadedFile after 5.2.15
because UploadedFile didn't pass test argument
https://github.com/laravel/framework/blob/5.2/src/Illuminate/Http/UploadedFile.php#L52

@vinkla
Copy link
Contributor

vinkla commented Feb 22, 2016

@CasperLaiTW Yeah, that is what made it work. Though, upgrading to 5.2.15 shouldn't break stuff. I know Laravel doesn't follow SemVer but it is quite hard to know when these kind of changes happen.

@imtiazPabel
Copy link

I was also fall on this problem in 5.2.16 and in now i am using 5.2.29 and this is solved

@albanafmeti
Copy link

albanafmeti commented Oct 2, 2017

@taylorotwell
Hey!
This issue is happening to me on Laravel 5.4, PHP 7, Ubuntu 16

After I move the uploaded file to a directory I make a call to a package class, but this package calls Request::createFromGlobals(); This is throwing the FileNotFoundException.

Example:

public function postStatus(Request $request)
{
    foreach ($request->allFiles() as $file) {
        $fileName = uniqid() . "_" . $file->getClientOriginalName();
        $file->move(storage_path("uploads/feeds"), $fileName);
    }

    // This line is throwing Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException
    // The file "/tmp/phpPQyWGk" does not exist
    $request = Request::createFromGlobals();

    dd($request->all());
}

// Help!

@jrnmagalhaes
Copy link

I got the same error. I discovered that my php.ini file was limiting my file upload to 2mb. I changed this and solved the error.

@waqarakbar
Copy link

waqarakbar commented Jul 7, 2018

I recently got same error on laravel 5.4 and php 5.6.4

(1/1) FileNotFoundException
The file "/Applications/MAMP/tmp/php/phpRYjYHA" does not exist

in MimeTypeGuesser.php line 116

my controller code looked like this


foreach ( $request->file( 'files' ) as $file ) {
        $fileName = sha1( $file->getClientOriginalName() ) . "-" . $complaint->id . "." . $file->guessExtension();
        $file->move( public_path( 'complaint_media' ), $fileName );
	$cins = [
		'title'        => $file->getClientOriginalName(),
		'file'         => $fileName,
		'media_type'   => ComplaintMedia::getMediaType( $file->guessExtension() )
	];
						
        $comp_media = ComplaintMedia::create( $cins );
}

But then I moved all method calls on $file (i.e. getClientOriginalName() and guessExtension()) before $file->move() and it works now. My controller code now looks like this

foreach ( $request->file( 'files' ) as $file ) {
	$original_name = $file->getClientOriginalName();
	$extension = $file->guessExtension();
	$file_name = sha1($original_name)."-" .time().".".$extension;
						
	$file->move(public_path('complaint_media'), $file_name);
						
	$cins = [
		'title'        => $original_name,
		'file'         => $file_name,
		'media_type'   => ComplaintMedia::getMediaType($extension)
	];
						
	$comp_media = ComplaintMedia::create($cins);
}

@Mokolos
Copy link

Mokolos commented Jul 20, 2018

think resetting $_FILES before redirecting or Request::createFromGlobals() would work :

$_FILES = array(); //reset fileBag
$request = Request::createFromGlobals();

@ekrokowski
Copy link

@Mokolos Thank you, indeed that worked.

@devflexpro
Copy link

@Mokolos you good man, worked

$_FILES = array(); //reset fileBag

@decadence
Copy link
Contributor

decadence commented Dec 6, 2019

Best option is just copy file so it will be on place for Request::createFromGlobals() and PHP deletes it in the end of the request.

@loko35387417
Copy link

face same issue, after i check this was caused by http request instance.
My code is $host = Request::capture()->getSchemeAndHttpHost()
I had changed to $request->getSchemeAndHttpHost()
It works fine.

@hendisantika
Copy link

I am using Laravel 7.8.1.
I am trying to upload the image files. Its uploaded to public/images.
But, when I try to retrieve the images it says that the image not found.
BTW, I am using MacOS Catalina.

Any suggestion??
Thanks

@hemratna
Copy link

@hendisantika I am facing the same issue. After moving a file to S3 not able to read from the $request->file('file')
Plz let me know if you found any workaround.

@ibrahimMH13
Copy link

face same issue, after i check this was caused by http request instance.
My code is $host = Request::capture()->getSchemeAndHttpHost()
I had changed to $request->getSchemeAndHttpHost()
It works fine.

where can i put this line ?

@ibrahimMH13
Copy link

think resetting $_FILES before redirecting or Request::createFromGlobals() would work :

$_FILES = array(); //reset fileBag
$request = Request::createFromGlobals();

put this line inside loop or top of controller ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests