Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

I have a form that sends a text file via POST method in Laravel 4. Inside the Controller, however, I can't figure out how to get its content in order to put it into a BLOB field in the DB.

Laravel's documentation and all the posts I found into the web, always show how to save the content into a file with the ->move() method.

This is my code in the Controller:

$book = Books::find($id); $file = Input::file('summary'); // get the file user sent via POST $book->SummaryText = $file->getContent(); <---- this is the method I am searching for... $book->save(); // save the summary text into the DB 

(SummaryText is a MEDIUMTEXT on my DB table).

So, how to get the file content in Laravel 4.1, without having to save it into a file? Is that possible?

like image 552
Rosario Russo Avatar asked Jan 27 '14 13:01

Rosario Russo


People also ask

What is $request in laravel?

Laravel's Illuminate\Http\Request class provides an object-oriented way to interact with the current HTTP request being handled by your application as well as retrieve the input, cookies, and files that were submitted with the request.

How can you retrieve the full URL for the incoming request in laravel?

The “path” method is used to retrieve the requested URI. The is method is used to retrieve the requested URI which matches the particular pattern specified in the argument of the method. To get the full URL, we can use the url method.


1 Answers

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()); 
like image 55
elitechief21 Avatar answered Oct 07 '22 01:10

elitechief21