[PSR-7] How to get the tmp_name of the uploaded file

1,512 views
Skip to first unread message

Oscar Otero

unread,
Jun 14, 2015, 1:30:42 PM6/14/15
to php...@googlegroups.com
Hi.
I'm trying to port some of my libraries to be compatible with psr-7 but I found a problem in the spec.

Form-manager is a simple library to manage forms in php. It provides php implementations to some html validation atributes such "required", "pattern", "min", "max", etc.

File inputs can have the "accept" attribute to limit the upload to some mime-types and, of course, there is a php implementation to ensure the uploaded files are right. You can see the implementation here:
https://github.com/oscarotero/form-manager/blob/master/src/Validators/Accept.php#L18

As you can see, I use the finfo extension of php to validate the file, because it's not recomended to use the client mime type. The problem is with the UploadedFileInterface I cannot access to the tmp_name of the file. A solution could be get the buffer of the file and use finfo-buffer but this cannot be done with large files.

I think this interface needs a method like $file->getServerFilename().

Mengdi Gao

unread,
Jun 14, 2015, 1:40:09 PM6/14/15
to php...@googlegroups.com
Try:

$resource = $uploadedFile->getStream()->detach()

--
You received this message because you are subscribed to the Google Groups "PHP Framework Interoperability Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to php-fig+u...@googlegroups.com.
To post to this group, send email to php...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/php-fig/74729d87-e3a4-46e3-85a9-6088900d4290%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Oscar Otero

unread,
Jun 14, 2015, 1:59:34 PM6/14/15
to php...@googlegroups.com
Sorry but this is not valid solution because:

- finfo only has support for buffers (finfo_buffer) and filenames (finfo_file) but not for stream resources
- on detach the resource, the stream (and uploadedFile instance) change to an inusable state, so the file cannot be copied or moved after that.

I only want to check the mime type of the file before move it.


Woody Gilk

unread,
Jun 14, 2015, 10:01:17 PM6/14/15
to php...@googlegroups.com
I'm not sure about this, but can't you use the $context parameter, like so: finfo_file(NULL, $options, $upload->getStream()) ?

Oscar Otero

unread,
Jun 15, 2015, 3:50:08 AM6/15/15
to php...@googlegroups.com
Well, I have found a solution using the metadata of the stream:

$tmp_file = $upload->getStream()->getMetadata('uri’);
$mime = finfo_file($finfo, $tmp_file);

Thanks.


desig...@gmail.com

unread,
Oct 19, 2018, 11:32:34 AM10/19/18
to PHP Framework Interoperability Group
This helped me with exif_imagetype as well. Thanks Oscar!
Reply all
Reply to author
Forward
0 new messages