OctoberCMS getPath用于多个文件

时间:2018-01-23 11:17:36

标签: laravel octobercms

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

我的模型看起来像这样:

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的结果。

任何人都知道我应该怎么做?

2 个答案:

答案 0 :(得分:1)

hmm可能是你的代码需要一点点修正,

我们也需要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
}
  

而不是使用$file->fileuploader->getPath()只需使用$file->getPath()

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

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

答案 1 :(得分:0)

如果我是你,我会在上传后保存我的文件,然后很容易保存路径以便您可以在其他程序中下载文件

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文件夹中,否则可能会出现权限问题