OctoberCMS getPath 多个文件

OctoberCMS getPath for multiple files

我有一个似乎无法解决的问题,我正在前端上传多个文件,但我需要获取这些文件的路径,以便我可以在另一个程序中下载文件。

我的模型是这样的:

public $attachMany = [
    'fileuploader' => 'System\Models\File'
];

然后我在组件上尝试了类似这样的愚蠢操作:

$quote->fileuploader = Input::file('fileuploader');

foreach ($quote->fileuploader as $file) {
            Db::table('auto_quotes')->where('quote_no', $quote->quote_no)->update(['path' => $file->fileuploader->getPath()]);
        }

但是我得到了 getPath = null 的结果。

有人知道我应该怎么做吗?

嗯,可能是您的代码需要稍作更正,

我想我们还需要 save $quote 才能使用它的 files

$quote->fileuploader = Input::file('fileuploader');

// save it before using
$quote->save();

foreach ($quote->fileuploader as $file) {
    Db::table('auto_quotes')
      ->where('quote_no', $quote->quote_no)
      ->update(['path' => $file->getPath()]); // <- correction
}

instead of using $file->fileuploader->getPath() just use $file->getPath()

因为我们已经在循环 $quote->fileuploader 并且每个文件都是 $file 所以我们不需要使用 $file->fileuploader 我们可以使用 $file 本身

如果仍有问题,请发表评论。

如果我是你,我会在上传后将我的文件保存在某个地方,这样就很容易保存路径这样你就可以在另一个程序中下载文件:

public function savePath($file)
{
    $file = Input::file('fileuploader');
    $destinationPath = ‘storage/uploads/yourFolder’;                        

    $file->move($destinationPath,$file->getClientOriginalName());
    Db::table('auto_quotes')->where('quote_no', $quote->quote_no)->update(['path' => $destinationPath]);
}

需要保存$destinationPath还是$destinationPath.$file->getClientOriginalName()

由你决定

PS:将文件存储在 storage 文件夹中,否则可能存在权限问题