php 如何在 Laravel 4 中获取通过 POST 发送的文件的内容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21381915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:06:08  来源:igfitidea点击:

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

phplaravel-4

提问by Rosario Russo

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 4 中的 POST 方法发送文本文件的表单。但是,在控制器内部,我无法弄清楚如何获取其内容以将其放入数据库中的 BLOB 字段中。

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.

Laravel 的文档和我在网上找到的所有帖子,总是展示如何使用->move()方法将内容保存到文件中。

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).

(SummaryText 是我的数据库表上的 MEDIUMTEXT)。

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

那么,如何在 Laravel 4.1 中获取文件内容,而无需将其保存到文件中?那可能吗?

回答by elitechief21

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

如果您要发布一个文本文件,那么它应该已经在服务器上。根据 laravel 文档,Input::file返回一个扩展 php 类的对象,SplFileInfo所以这应该可以工作:

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

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

我不确定 php 方法file_get_contents是否可以在 Laravel 框架中工作......如果它不尝试这个:

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

回答by Krzysztof Duszczyk

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

更好的解决方案而不是 file_get_contents 将使用 SPL 类方法,因为 FileUpload 已经扩展了它们。

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

To read more about SplFileInfo and SplFileObject see:

要阅读有关 SplFileInfo 和 SplFileObject 的更多信息,请参阅:

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

因为这些可能非常有用,并且使用 SPL(OOP)是比结构化 PHP 函数更好的解决方案。

回答by Roland Starke

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

从 Laravel v5.6.30 开始,您可以获取上传文件的文件内容,例如:

use Illuminate\Http\Request;

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

source: this commit

来源:这次提交

回答by DoverAudio

\File::get($directory.$filename);

Worked for my project, where directory and filename are determined by the upload methods. The backslash is used when creating workspaces (packages) in laravel4.

为我的项目工作,其中目录和文件名由上传方法确定。在 laravel4 中创建工作区(包)时使用反斜杠。