Hello!
Today I’m trying to work with upload files.
How can I get property $file?
It is protected property. Have any method fo it?
“Cannot access protected property Slim\Psr7\UploadedFile::$file”
Thank you
Have you tried this?
http://www.slimframework.com/docs/v4/cookbook/uploading-files.html
The moveTo option is not suitable.
I’m downloading this file on the Amazon cloud, and I don’t need to move it using “moveTo”. To do this, I need the file itself. So that you can do file_get_contents.
But in the UploadedFile class there is no way to take tmp_name.
In the application itself, so far I have done file_get_content ($ FILES [‘my_file’] [‘tmp_name’]).
This works, but is not suitable for writing autotests. And in general, it somehow seems wrong.
In PSR-7 it’s an stream. To fetch the content of the stream as string try this.
use Psr\Http\Message\UploadedFileInterface;
// ...
/** @var UploadedFileInterface[] $uploadedFiles */
$uploadedFiles = $request->getUploadedFiles();
// The array index depends on your upload form field
$uploadedFile = $uploadedFiles['example1'];
if ($uploadedFile->getError() === UPLOAD_ERR_OK) {
$targetFile = '/var/tmp/filename.ext';
//$filename $uploadedFile->getClientFilename();
// Convert the stream to string
$content = (string)$uploadedFile->getStream();
// Save the content to file
file_put_contents($targetFile, $content);
}
2 Likes
It saved me. Thank you very much
1 Like