草庐IT

php - Codeigniter - 将 2 个文件附加到已从用户表单上传到网络服务器的电子邮件

coder 2024-05-01 原文

我有一个表格,填写后会上传 2 个文件(一个是 doc、docx 或 pdf,另一个是图像文件)。我的 Controller 验证表单 ok 并将文件上传到服务器。我的问题似乎是访问上传文件的数组!?

我在表单中的一个名为 $uploaded 的变量中引用了该数组,从错误看来,实际数组似乎被称为“成功”(我认为它是从 upload.php 库中获取这个名称的)我认为然后这包含 2 个元素,称为 [0] 和 [1],每个元素包含另一个文件属性数组。

我基本上是在兜圈子,试图将这两个文件附加到电子邮件中。我希望有人能够帮助新手?我将 Controller 代码放在下面,如果您需要任何其他代码,请告诉我。

编辑:我更新了下面的代码 我现在取得了一些成功(不多):for 循环现在附加了 1 个文件,但是附加了两次。所以要么我的代码没有正确索引数组,要么数组没有存储两个文件信息?

<?php
class Form extends CI_Controller {

    function index()
    {
        $this->load->helper(array('form', 'url'));

        $this->load->library('form_validation');

        $this->form_validation->set_rules('first_name', 'First Name', 'required|alpha');
        $this->form_validation->set_rules('last_name', 'Surname', 'required|alpha');
        $this->form_validation->set_rules('dob', 'Date of Birth', 'required');
        $this->form_validation->set_rules('nationality', 'Nationality', 'required|alpha');
        $this->form_validation->set_rules('gender', 'Gender', 'required');
        $this->form_validation->set_rules('address_l1', 'Address Line 1', 'required|alpha_dash_space');
        $this->form_validation->set_rules('address_l2', 'Address Line 2', 'alpha');
        $this->form_validation->set_rules('address_city', 'City', 'required|alpha');
        $this->form_validation->set_rules('address_postcode', 'Post Code', 'required|alpha_dash_space');
        $this->form_validation->set_rules('address_country', 'Country', 'required|alpha_dash_space');
        $this->form_validation->set_rules('e_address', 'Email Address', 'required|valid_email');
        $this->form_validation->set_rules('h_tel', 'Home Telephone Number', 'required|numeric');
        $this->form_validation->set_rules('mobile', 'Mobile Number', 'required|numeric');
        $this->form_validation->set_rules('university', 'University', 'required|alpha_dash_space');
        $this->form_validation->set_rules('campus', 'Campus Name', 'required|alpha_dash_space');
        $this->form_validation->set_rules('course', 'Course Title', 'required|alpha_dash_space');
        $this->form_validation->set_rules('end_date', 'Course End Date', 'required');

        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('home');
        }
        else
        {

            //Display Success page
            $this->load->view('formsuccess');

            //Send Email
            $this->load->library('email');

            //Array helper
            $this->load->helper('array');

            //Upload files to server
            $this->load->library('upload');

            $config['upload_path']   = './attachments'; //if the files does not exist it'll be created
            $config['allowed_types'] = 'gif|jpg|png|doc|docx|txt|pdf';
            $config['max_size']   = '4000'; //size in kilobytes
            $config['encrypt_name']  = TRUE;

            $this->upload->initialize($config);

            $uploaded = $this->upload->up(FALSE); //Pass true if you want to create the index.php files

            $data = $this->upload->data();          

            for ($i = 0; $i <= 1; $i++) {
                // ignore file that have not been uploaded
                //if (empty($_FILES['uploaded'.$i])) continue;

                //get the data of the file
                //$fileName = $data['uploaded'.$i]['name'];
                $filepath = $data['full_path'];
                //add only if the file is an upload
                echo $data['full_path'];
                $this->email->attach($filepath); //$mail->AddAttachment($filePath, $fileName);
            }


            $this->email->from('your@example.com', 'Your Name');
            $this->email->to('t.bryson@shu.ac.uk'); 

            $this->email->subject('Application for Accommodation (Non-SHU)');
            $this->email->message('Testing the email class.');  

            $this->email->send();

            echo $this->email->print_debugger();
        }
    }
    function alpha_dash_space($str_in)
    {
    if (! preg_match("/^([-a-z0-9_ ])+$/i", $str_in)) {
    $this->form_validation->set_message('_alpha_dash_space', 'The %s field may only contain alpha-numeric characters, spaces, underscores, and dashes.');
    return FALSE;
    } else {
    return TRUE;
    }
    }
}
?>

最佳答案

最终在 google 的帮助下解决了这个问题:

$uploaded = $this->upload->up(FALSE); //Pass true if you want to create the index.php files
        var_dump($uploaded); 
        $data = array('uploaded_data');

        //Attach the 2 files to email
        foreach($_FILES as $key => $value){
            //var_dump($uploaded['success'][$key]['full_path']); //FOR TESTING
            $file = $uploaded['success'][$key]['full_path'];
            $this->email->attach($file);
            //unlink($file);
        }

关于php - Codeigniter - 将 2 个文件附加到已从用户表单上传到网络服务器的电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15090421/

有关php - Codeigniter - 将 2 个文件附加到已从用户表单上传到网络服务器的电子邮件的更多相关文章

  1. ruby - 用 Ruby 编写一个简单的网络服务器 - 2

    我想在Ruby中创建一个用于开发目的的极其简单的Web服务器(不,不想使用现成的解决方案)。代码如下:#!/usr/bin/rubyrequire'socket'server=TCPServer.new('127.0.0.1',8080)whileconnection=server.acceptheaders=[]length=0whileline=connection.getsheaders想法是从命令行运行这个脚本,提供另一个脚本,它将在其标准输入上获取请求,并在其标准输出上返回完整的响应。到目前为止一切顺利,但事实证明这真的很脆弱,因为它在第二个请求上中断并出现错误:/usr/b

  2. ruby-on-rails - 如何将大于 5GB 的文件上传到 Amazon S3? - 2

    我目前正在使用带有Carrierwavegem的Rails3.2将文件上传到AmazonS3。现在我需要能够处理用户提交的大于5GB的文件,同时仍然使用Carrierwavegem。Carrierwave或Fog是否有任何其他gem或分支可以处理5GB以上的文件上传到S3?编辑:我不想重写一个完整的Rails上传解决方案,所以像这样的链接没有帮助:https://gist.github.com/908875. 最佳答案 我想出了如何做到这一点,并且现在可以正常工作了。在正确的config/environment文件中,添加以下内容以

  3. ruby-on-rails - 验证电子邮件地址是 Paypal 用户 - 2

    我想验证一个电子邮件地址是否是PayPal用户。是否有API调用来执行此操作?是否有执行此操作的ruby​​库?谢谢 最佳答案 GetVerifiedStatus来自PayPal'sAdaptiveAccounts平台会为您做这件事。PayPal没有任何codesamples或SDKs用于Ruby中的自适应帐户,但我确实找到了编写codeforGetVerifiedStatusinRuby的人.您需要更改该代码以检查他们拥有的帐户类型的唯一更改是更改if@xml['accountStatus']!=nilaccount_status

  4. ruby-on-rails - Ruby on Rails - 需要在每周的特定时间将消息发送到电子邮件 - 2

    我想知道我应该如何着手这个项目。我需要每周向人们发送一次电子邮件。但是,这必须在每周的特定时间自动生成并发送。编码有多难?我需要知道是否有任何书籍可以提供帮助,或者你们中的任何人是否可以指导我。它必须使用ruby​​onrails进行编程。因此有一个网络服务和数据库集成。干杯 最佳答案 为什么这么复杂?您只需安排工作。您可以使用Delayed::Job例如。Delayed::Job让您可以使用run_at符号在特定时间安排作业,如下所示:Delayed::Job.enqueue(SendEmailJob.new(...),:run_

  5. ruby - 使用 Ruby 开发工具包将文件上传到 Amazon S3 - 2

    我正在尝试上传文件。一个简单的hello.txt。我正在关注文档,但无法将其上传到我的存储桶。#STARTAWSCLIENTs3=Aws::S3::Resource.newbucket=s3.bucket(BUCKET_NAME)begins3.buckets[BUCKET_NAME].objects[KEY].write(:file=>FILE_NAME)puts"Uploadingfile#{FILE_NAME}tobucket#{BUCKET_NAME}."bucket.objects.eachdo|obj|puts"#{obj.key}=>#{obj.etag}"endresc

  6. ruby-on-rails - 这个 C 和 PHP 程序员如何学习 Ruby 和 Rails? - 2

    按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭9年前。我来自C、php和bash背景,很容易学习,因为它们都有相同的C结构,我可以将其与我已经知道的联系起来。然后2年前我学了Python并且学得很好,Python对我来说比Ruby更容易学。然后从去年开始,我一直在尝试学习Ruby,然后是Rails,我承认,直到现在我还是学不会,讽刺的是那些打着简单易学的烙印,但是对于我这样一个老练的程序员来说,我只是无法将它

  7. ruby - 使用ruby mail gem获取电子邮件正文而不获取附件 - 2

    我正在尝试使用IMAP和ruby​​mailgem获取gmail的正文。当我按照anotherstackoverflowanswer.中的描述获取RFC822字段时,它工作得很好.Fiedl很好地描述了这种方法answer类似的问题。这种方法很棒,只是它需要获取RFC822,而RFC822也会获取所有附件。是否有任何其他领域或其他方法我可以采取不获取附件但仍然使用ruby​​mailgem来获得解码良好的正文? 最佳答案 您必须解析并理解返回的BODYSTRUCTURE响应的实际结构,请参阅RFC3501,p.56.还要记住应用相关

  8. ruby - 使用 S/MIME 在 Ruby 中对电子邮件进行数字签名 - 2

    Ruby中是否有一种方法可以使用S/MIME对电子邮件消息进行数字签名?我们的团队使用PKI,我们的用户习惯于期待重要消息的数字签名。我知道我可以调用openssl命令行工具:opensslsmime-sign-signer$CERT_FILE-passinpass:$CERT_PASS-in$UNSIGNED_MAIL-out$SIGNED_MAIL-certfile$CERT_CA_FILE-from'your'-to'recipients'-subject'TheSubject'但我希望利用Ruby解决方案。 最佳答案 我最终

  9. ruby-on-rails - 带有内联附件的 Rails 邮件程序错误 - 2

    我在ActionMailer中有一个非常罕见的行为,我在5个月前实现了一个邮件程序操作并且它工作正常,但昨天,由于一些奇怪的原因,它崩溃了。问题我有一个邮件布局,为了在我所有的电子邮件中使用它,我在其中渲染了一张之前由前置过滤器附加的图像Layout=app/views/layouts/email.html.erbVisionamos>......用户邮件程序=app/mailers/users.rbclassUsuariosMailer"monitoreo@visionamos.com"layout"mail"#Setemaillayoutbefore_filter:add_inli

  10. ruby-on-rails - 发生异常时发送电子邮件不起作用,使用 exception_notification - 2

    我正在从rails2.3迁移到rails3.1,我试图在生成异常时发送电子邮件。我正在使用exception_notificationgem。我的其余电子邮件都在工作。但是异常邮件不会被解雇。以下是我的staging.rb文件中的设置。config.action_mailer.perform_deliveries=trueconfig.action_mailer.raise_delivery_errors=true下面是application.rb中的代码C::Application.config.middleware.useExceptionNotification::Rack,:e

随机推荐