【问题标题】:OctoberCMS getPath for multiple files多个文件的 OctoberCMS getPath
【发布时间】:2018-01-23 11:17:36
【问题描述】:

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

我的模型如下所示:

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

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

【问题讨论】:

  • 是否可以先让文件上传并保存该模型,然后您可以使用路径更新auto_quotes 模型?意味着您可以在上传文件并保存相关的quote 模型后在磁盘上获取路径

标签: laravel octobercms


【解决方案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-&gt;fileuploader-&gt;getPath(),而是使用$file-&gt;getPath()

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

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

【讨论】:

  • 感谢您的建议,我尝试过但收到错误消息:Method getPath does not exist.
  • $quote-&gt;save(); 你能在loop 之前添加这个代码吗?我在答案中添加了它。请检查并告诉我
【解决方案2】:

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

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-&gt;getClientOriginalName()

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-13
    • 2012-08-14
    • 1970-01-01
    • 2018-12-06
    • 1970-01-01
    相关资源
    最近更新 更多