Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot get file contents on UploadedFile Symfony

Tags:

symfony

I have the following function definition:

public function save(UploadedFile $file, string $fileSystemName)
{
    $fs = $this->fileSystemMap->get($fileSystemName);

    $contents = file_get_contents($file->getRealPath());
    $filename = sprintf('%s/%s/%s/%s.%s', date('Y'), date('m'), date('d'), uniqid(), $file->getClientOriginalExtension());

    $fs->write($fileName, $contents);
}

When the code runs:

file_get_contents($file->getRealPath());

It throws an error saying:

Warning: file_get_contents(/tmp/phpM9Ckmq): failed to open stream: No such file or directory

Note that I also tried to use $file->getPathName(), but the result is just the same.

Why is this happening?

Thanks!

like image 239
iamjc015 Avatar asked Nov 25 '17 15:11

iamjc015


1 Answers

Simplest way to read content of uploaded file is :

  public function index(Request $request)
{  $raw='';

    if ($request->getMethod() == "POST") {
        $files = $request->files->all();
        foreach ($files as $file) {
            if ($file instanceof UploadedFile) {
                $raw .= file_get_contents($file->getPathname());

            }
        }

    }

    return $this->render('main/index.html.twig', [
        'controller_name' => 'MainController',
    ]);
}

your data will be stored in $raw

like image 100
Michał G Avatar answered Sep 27 '22 22:09

Michał G