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

pascalb's avatar

Retrieve file from storage and cast it to UploadedFile

Hi all, My question is fairly simple and for some reason I can't figure it out. I have files stored in the storage folder in my app and sometimes, I want to be able to retrieve one of these existing files and use it has a UploadedFile object in my code.

I manage to get the raw string contents of the file using the Storage::get method but how can I initialize a new UploadedFile object from that string ? Basically, I want to be able to play with the file the same way I would do with a file coming from a form request.

Any help would be greatly appreciated!

0 likes
8 replies
ohffs's avatar

I don't think you can do that "out of the box" as (at a guess) it's calling is_uploaded_file() somewhere. You might be able to do something like :

class MyUploadedFile extends UploadedFile
{
  public function isValid()
  {
    return true;
  }
}

I'm guessing though :-) You could try passing the info to the constructor and see what happens I suppose :-)

pascalb's avatar

Hey @ohffs ,

Thanks for your time, yeah you actually guide me in the right direction. I did it by initiating a new object from the constructor.

Here is a sample of my code, hope it can help someone in the futur.

    use finfo;
    
        $file_path = storage_path($filename);
        $finfo = new finfo(FILEINFO_MIME_TYPE);

        if (Storage::disk('local')->exists('path/' . $filename)) {

            return new UploadedFile(
                $file_path,
                $filename,
                $finfo->file($file_path),
                filesize($file_path),
                0,
                false
            );
        }

3 likes
ohffs's avatar

Yay - glad you got a solution :-)

pascalb's avatar

@pmall

I need to extract some text from a stored file on update and I want my function to return a UploadedFile object mostly for code reuse because the extraction method is the same on create and on update.

I do this for maintenance purposes, so this way I can always be sure that I am working with a UploadedFile object through out the rest of the code. But you are right I could have done without it and maybe I will.

pmall's avatar

mostly for code reuse because the extraction method is the same on create and on update.

@chess So why dont you just extract this code into another class, and make a method taking a file as an argument to do this process (a file, not an uploaded file, this is more flexible, because an uploaded file is just an extended file). Then you can use this class anywhere you need to process a file, wherever it comes from.

A controller method (I guess it is what you want to reuse) is not made for code reuse / being called as a regular function, it is made for handling a request. But what's inside a controller can be reused anywhere if coded properly.

1 like
pascalb's avatar

@pmall

" So why dont you just extract this code into another class, and make a method taking a file as an argument to do this process (a file, not an uploaded file, this is more flexible, because an uploaded file is just an extended file). Then you can use this class anywhere you need to process a file, wherever it comes from. "

Exactly what I did, this code was not for a controller method.

pmall's avatar

So why cant you make it work with both a file and an uploaded file ?

1 like

Please or to participate in this conversation.