十月CMS获取多个文件的路径



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

我的模型如下所示:

public $attachMany = [
    'fileuploader' => 'SystemModelsFile'
];

然后我在组件上尝试了这样愚蠢的事情:

$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
}

与其使用$file->fileuploader->getPath()不如使用$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文件夹中,如果没有,您可能会遇到权限问题

最新更新