Php OctoberCMS表单文件上载|为foreach()提供的参数无效

Php OctoberCMS表单文件上载|为foreach()提供的参数无效,php,laravel,laravel-5,octobercms,october-form-controller,Php,Laravel,Laravel 5,Octobercms,October Form Controller,我对OctoberCMS&Laravel相当陌生。我已经建立了一个联系方式,除了添加多个文件附件外,一切都很好。我在网上进行了搜索,但未能找到解决我的表单无法发送的原因的方法 问题中的错误是“第26行为foreach()提供的参数无效” 第26行是第一个foreach循环 我在下面附上了表单html标记以及表单的控制器 PHP代码 <?php namespace Bprint\Contact\Components; use Cms\Classes\ComponentBase; use In

我对OctoberCMS&Laravel相当陌生。我已经建立了一个联系方式,除了添加多个文件附件外,一切都很好。我在网上进行了搜索,但未能找到解决我的表单无法发送的原因的方法

问题中的错误是“第26行为foreach()提供的参数无效”

第26行是第一个foreach循环

我在下面附上了表单html标记以及表单的控制器

PHP代码

<?php
namespace Bprint\Contact\Components;
use Cms\Classes\ComponentBase;
use Input;
use Mail;
class ContactForm extends ComponentBase{
    public function componentDetails(){
        return [
            'name' => 'Contact Form',
            'description' => 'Bprint Contact Form'
        ];
    }
    public function onSend(){
        $vars = ['fieldName' => Input::get('fieldName'), 'fieldCompany' => Input::get('fieldCompany'), 'fieldEmail' => Input::get('fieldEmail'), 'fieldUpload' => Input::file('fieldUpload'), 'fieldContent' => Input::get('fieldContent')];
        $attachments = [];
        foreach($vars['fieldUpload'] as $image) {
            $attachments[] = $image;
        }
        Mail::send('bprint.contact::mail.message', $vars, function($message) use ($attachments) {  
            foreach($attachments as $attachment) {
                $params = ['as' => $attachment->getFilename()/*, 'mime' => $mime*/];
                $message->attach($attachment->getLocalPath(), $params);
            }
            $message->to('REDACTED', 'BlueprintCMS');
            $message->subject('Blueprint Website | Contact Form"');
        });
    }
}

您使用的是OctoberCMS的ajax框架,如果您想使用此表单上载文件附件,则需要向表单元素添加一个属性数据请求文件

您的表单元素将如下所示:

<form data-request="onSend"
    data-request-files
    data-request-error="alert('There was an error sending your message. Please try again shortly, if error persits please email us at blueprint@outoftheblue.org.uk')"
    data-request-success="alert('Your message has been sent.')"
    class="bpsite-form floating-labels">


它将开始工作。

通常,在上载文件/图像时,您需要在
元素上添加
enctype=“multipart/form data”
。另外,
Input::file(“fileUpload”,[])->传递
[]
将防止在未上载文件时出现问题;空数组而不是
null
。谢谢你,蒂姆,这确实有帮助。非常感谢,太棒了!不知何故,我完全忘记了ajax数据属性,我不再有任何错误,电子邮件也不会发送,但是当电子邮件到达时,它没有实际附加的附件,是否有任何内容需要添加到我的邮件视图中以实际发送文件?使用此$attachment->getPathName()要获取附件的本地路径,在此之后,您可以将此答案向上投票并标记为正确。thanks@JackSALVONA嘿,杰克,正如你所说,这个答案是正确的。你能把它标对并投票吗?????你好,这实际上并没有纠正我的问题,它有帮助,但问题还没有解决。我的邮件没有附加到电子邮件,但我不再有PHP错误。我想试试Octorbercms论坛。@JackSALVONA你的实际问题是关于表单文件上传的(我的解决方案很完美;),但是你在评论中问了另一个问题,所以你需要一个答案,你必须在stackoverflow中问一个单独的问题。
<form data-request="onSend"
    data-request-files
    data-request-error="alert('There was an error sending your message. Please try again shortly, if error persits please email us at blueprint@outoftheblue.org.uk')"
    data-request-success="alert('Your message has been sent.')"
    class="bpsite-form floating-labels">