How to get the content of a file sent via POST in Laravel 4?

If you're posting a text file to than it should already be on the server. As per the laravel documentation, Input::file returns an object that extends the php class SplFileInfo so this should work:

$book->SummaryText = file_get_contents($file->getRealPath());

I'm not sure if the php method file_get_contents will work in the Laravel framework...if it doesn't try this:

$book->SummaryText = File::get($file->getRealPath());

Since Laravel v5.6.30 you can get the file content of an uploaded file like:

use Illuminate\Http\Request;

Route::post('/upload', function (Request $request) {
    $content = $request->file('photo')->get();
});

source: this commit


Nicer solution instead of file_get_contents will be to use the SPL class methods as FileUpload is already extending them.

$file = Input::file('summary')->openFile();
$book->SummaryText = $file->fread($file->getSize());

To read more about SplFileInfo and SplFileObject see:

  • http://php.net/manual/en/class.splfileinfo.php
  • http://php.net/manual/en/class.splfileobject.php

As those could be really usefull and using SPL which is OOP is nicer solution than structural PHP functions.

Tags:

Php

Laravel 4