This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use Stack Overflow.

create File object using memory to store the file content

New topics about Symfony 2 should go here

create File object using memory to store the file content

by jion1987 » Wed May 27, 2015 2:51 pm

I have a blob resource from my db. I want to wrap temporaly this file into Symfony File object because i want to use specific methods like the extension guesser, and apply symfony file validators.
I want to store this temporary file into memory, because the blobs are small files and i dont want to create a file in disk in every request.

I tried to do this in that way:

Code: Select all
$file = new File ('php://temp');


but symfony throws an error that says 'The file "php://temp" does not exist'. Looking at File source, the error is caused by a "is_file($path)" check that is made in the constructor, and I can invalidate this putting false in the second argument. But, if I do:

Code: Select all
$file = new File ('php://temp', false);


the File is created, but then the error comes back later, e.g. when i use the guesser:

Code: Select all
$file->guessExtension($file)


because in Symfony/Component/HttpFoundation/File/MimeType/MimeTypeGuesser.php:

Code: Select all
public function guess($path)
    {
        if (!is_file($path)) {
            throw new FileNotFoundException($path);
        }
 (...)


Ok. Then my question is: There is a way to load a 'php://temp' or 'php://memory' within a File object?
jion1987
Junior Member
 
Posts: 1
Joined: Wed May 27, 2015 2:46 pm