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?
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.
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.
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());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With