如何在 Laravel 5.5 中获取变量中的文件内容

How to get the content of the file in variable in Laravel 5.5

我正在上传需要通过 office365 附加到电子邮件的文件 API。

我需要的是变量中的文件内容,但没有 storing/saving 文件,我该怎么做?

foreach ($request->filesToUpload as $file) {
    $originalName = $file->getClientOriginalName();//working
    $content = $file->getContent();//<- I need this, but not working
    //$this->addAttachment($content, $originalName) //logic for later
}

像这样访问文件的内容:

$content = file_get_contents(Input::file('nameAttribute')->getRealPath());

或者换句话说在那个循环中

$contents = file_get_contents($file->getRealPath());

通过对象方法获取真实路径,您可以像与任何其他文件一样与之交互。

Waqas Bukhary, 你得到那个结果,因为 getContent 不是 uploadedFiles 的方法,检查 documentation。 但是你有路径,所以你总是可以阅读内容,比如:

$path = $file->path();
$handle = fopen($path, 'r');
$content = fread($handle, filesize($path);
fclose($handle);

如果您知道文件字段的名称,您也可以使用请求文件方法,检查它here