Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Phlisg's avatar

PHP or Laravel file storage "async"

Hello all,

Using Laravel & Nova in a company project, I am hitting a brickwall in regards of uploading images and php's treatment to save them.

So, my context:

  • Media extends Model and has a polymorphic many-to-many relation with multiple other models
  • On these multiple other models, considering the nature of a Many-To-Many polymorphic relation on Nova, I need to create an action if one wants to add an image to a resource from its details view
  • Said action is what gives me this issue:

What I'm doing

class AddNewMedia extends Action
{
    /**
     * Perform the action on the given models.
     *
     * @param ActionFields $fields
     * @param Collection $models
     *
     * @return mixed
     */
    public function handle(ActionFields $fields, Collection $models)
    {
        $file = $fields->file_name->storePublicly('medias'); // saves the file in root/storage/app/medias/<filename>

        foreach ($models as $model) {
	    /* attaching models & other shenanigans that work fine */
            $this->markAsFinished($model);
        }

        return Action::message('Media added!');
    }

    public function fields(){ /* ImageField named 'file_name' */ }
}

What's happening

Once the user submits the Action, everything works great - Nova doesn't complain, but I end up with a broken "image preview". However, on the back side of the story, in the Logs: I notice some Exceptions happening about a "file not found", like so:

[2020-03-25 14:37:21] local.ERROR: unable to open image `/var/www/projects/laravel/[...]/storage/app/public/medias/bwC6bfqEmOlaQ1Y1Ehi2MmAJTbzQXCEMNGtkDdsT.jpeg': No such file or directory 

But...

| (the fun begins!)

As I switch back to PhpStorm, I see the image with the mentionned filename in the logs innocently popping up in the correct folder!

The (what I think) identified problem:

$file = $fields->file_name->storePublicly('medias'); // This function runs "async" or is "lazy"

This method used here, just waits until whatever code follows to finally store the image.

I've read something about stream() here - Stackoverflow, but I am unsure how to use that in my Action...

I tried everything. A sleep(), a while(){} until finally the file arrives in the correct folder, nothing works. The image pops in mentioned folder seemingly after PHP is done with the rest of the code that concerns that specific image.

This is a headache. I want this storing function to be sync, and I've spent 2h on the web not finding any issue related to that, as if I'm the only one needing sync files saving.

Hast thou the solution?

I'm unsure what to do. Is there a secret method I could use? Should I turn the rest of the action into a Job? (unnecessarily complex), or could it be Docker playing me a lovely trick? (unfortunately the latter still happens on a real server not running my code on Docker)

Thank you all for your englightenment. I'm sure I'm not the only one with this issue.

0 likes
1 reply
Doougui's avatar

I'm running into the same problem here :/ Did you manage to find a solution?

Please or to participate in this conversation.