Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load a 'php://temp' or 'php://memory' file within a Symfony File object

Tags:

file

php

symfony

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:

$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:

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

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

$file->guessExtension($file)

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

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?

like image 916
jion Avatar asked May 27 '15 15:05

jion


1 Answers

Pretty sure php://temp writes to memory until it is full and then writes to a file, whereas php://memory ensures only in memory with no fall back.

like image 89
Beld Avatar answered Nov 15 '22 06:11

Beld