简体   繁体   English

附加目录中的文件并使用codeigniter发送至电子邮件

[英]attach a file from directory and send to email with codeigniter

I am a newbie with codeigniter and I want to build a website using codeigniter framework. 我是Codeigniter的新手,我想使用Codeigniter框架构建网站。 From first, it look fine I can use database, validation, email, session and then I try to attach a file and send with email : 首先,看起来不错,我可以使用数据库,验证,电子邮件,会话,然后尝试附加文件并通过电子邮件发送:

$this->email->attach('/path/ofyour/constan/file.anything'); $ this-> email-> attach('/ path / ofyour / constan / file.anything');

thats work too. 多数民众赞成在工作。

since that is a website I want my client to choose file they want to upload. 因为这是一个网站,所以我希望我的客户选择他们要上传的文件。 I try many method, and many of them tell to upload a file to server root and get the file_data, use file_data[file_patch] 我尝试了许多方法,其中许多方法告诉将文件上传到服务器根目录并获取file_data,使用file_data [file_patch]

$this->email->attach('file_data[file_path]'); $ this-> email-> attach('file_data [file_path]');

the problem is: 问题是:

  1. since code igniter cant upload multiple data I must use plugin. 由于代码点火器无法上传多个数据,因此我必须使用插件。 I tried and its PAIN 我尝试了它的痛苦

  2. I thing its not effective, upload data to server root and then to email? 我觉得它没有效果,将数据上传到服务器根目录,然后再发送到电子邮件?

  3. it better to just get file_path of upload file and send them to email, how? 最好只获取上传文件的file_path并将其发送到电子邮件,怎么办?

I build it with jquery mobile, what must I do? 我使用jquery mobile构建它,该怎么办?

Update 更新资料

ok i decide to use uploadify i search every website and then i found here and my code is 好的,我决定使用uploadify来搜索每个网站,然后在这里找到,我的代码是

uploadify.php uploadify.php

<?php
/*
*   Functions taken from CI_Upload Class
*
*/

    function set_filename($path, $filename, $file_ext, $encrypt_name = FALSE)
    {
        if ($encrypt_name == TRUE)
        {       
            mt_srand();
            $filename = md5(uniqid(mt_rand())).$file_ext;   
        }

        if ( ! file_exists($path.$filename))
        {
            return $filename;
        }

        $filename = str_replace($file_ext, '', $filename);

        $new_filename = '';
        for ($i = 1; $i < 100; $i++)
        {           
            if ( ! file_exists($path.$filename.$i.$file_ext))
            {
                $new_filename = $filename.$i.$file_ext;
                break;
            }
        }

        if ($new_filename == '')
        {
            return FALSE;
        }
        else
        {
            return $new_filename;
        }
    }

    function prep_filename($filename) {
       if (strpos($filename, '.') === FALSE) {
          return $filename;
       }
       $parts = explode('.', $filename);
       $ext = array_pop($parts);
       $filename    = array_shift($parts);
       foreach ($parts as $part) {
          $filename .= '.'.$part;
       }
       $filename .= '.'.$ext;
       return $filename;
    }

    function get_extension($filename) {
       $x = explode('.', $filename);
       return '.'.end($x);
    } 


// Uploadify v1.6.2
// Copyright (C) 2009 by Ronnie Garcia
// Co-developed by Travis Nickels
if (!empty($_FILES)) {
    $path = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
   //$client_id = $_GET['client_id'];
   $file_temp = $_FILES['Filedata']['tmp_name'];
   $file_name = prep_filename($_FILES['Filedata']['name']);
   $file_ext = get_extension($_FILES['Filedata']['name']);
   $real_name = $file_name;
   $newf_name = set_filename($path, $file_name, $file_ext);
   $file_size = round($_FILES['Filedata']['size']/1024, 2);
   $file_type = preg_replace("/^(.+?);.*$/", "\\1", $_FILES['Filedata']['type']);
   $file_type = strtolower($file_type);
   $targetFile =  str_replace('//','/',$path) . $newf_name;
   move_uploaded_file($file_temp,$targetFile);

   $filearray = array();
   $filearray['file_name'] = $newf_name;
   $filearray['real_name'] = $real_name;
   $filearray['file_ext'] = $file_ext;
   $filearray['file_size'] = $file_size;
   $filearray['file_path'] = $targetFile;
   $filearray['file_temp'] = $file_temp;
   //$filearray['client_id'] = $client_id;

   $json_array = json_encode($filearray);
   echo $json_array;
}else{
    echo "1";   
}

i dont relly know what is going on here, like i said i am a newbie but i know something that $json_array, that array hold my data $filearray, that is data file uploaded. 我不知道这里发生了什么,就像我说我是新手一样,但我知道$ json_array,该数组保存我的数据$ filearray,即上传的数据文件。 mission one complete 任务一完成

now my controller: upload.php 现在我的控制器是:upload.php

<?php
class Upload extends CI_Controller
{
public function __construct()
    {
        parent::__construct();

                $this->load->helper('form');
                $this->load->helper('url');

    }
    /*
    *   Display upload form
    */
    function index()
    {

        $this->load->view('view');
    }


    /*
    *   Handles JSON returned from /js/uploadify/upload.php
    */
    function uploadify()
    {

        //Decode JSON returned by /js/uploadify/upload.php
        $file = $this->input->post('filearray');
        $data['json'] = json_decode($file);

        $this->load->view('uploadify',$data);
    }

}
/* End of File /application/controllers/upload.php */

my plan is send the data in onComplete function 我的计划是在onComplete函数中发送数据

my view :view.php 我的看法:view.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<<head>
    <meta charset="UTF-8">
    <title>Uploadify and Codeigniter Tutorial</title>
<?php
   $this->load->helper('html');
   echo link_tag('http://uploadify_tutorial/uploadify/uploadify.css');
   echo '<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js" type="text/javascript"></script>';
   echo '<script src="http://localhost/uploadify_tutorial/uploadify/swfobject.js" type="text/javascript"></script>';
   echo '<script src="http://localhost/uploadify_tutorial/uploadify/jquery.uploadify.v2.1.4.min.js" type="text/javascript"></script>';
$uploadpath="";
$uploadpath=str_ireplace($_SERVER['DOCUMENT_ROOT'],"", realpath($_SERVER['SCRIPT_FILENAME']));
$uploadpath=str_ireplace("index.php","",$uploadpath);
?>


    <script type="text/javascript" language="javascript">
        $(document).ready(function(){

                    $("#upload").uploadify({
                            uploader: '<?php echo base_url();?>uploadify/uploadify.swf',
                            script: '<?php echo base_url();?>uploadify/uploadify.php',
                            cancelImg: '<?php echo base_url();?>uploadify/cancel.png',
                            folder: '/uploads',
                            scriptAccess: 'always',
                            multi: true,
                            'onError' : function (a, b, c, d) {
                                 if (d.status == 404)
                                    alert('Could not find upload script.');
                                 else if (d.type === "HTTP")
                                    alert('error '+d.type+": "+d.status);
                                 else if (d.type ==="File Size")
                                    alert(c.name+' '+d.type+' Limit: '+Math.round(d.sizeLimit/1024)+'KB');
                                 else
                                    alert('error '+d.type+": "+d.text);
                                },
                            'onComplete'   : function (event, queueID, fileObj, response, data) {
                                                //Post response back to controller
                                                $.post('<?php echo site_url('upload/uploadify');?>',{filearray: response},function(info){
                                                    $("#target").append(info);  //Add response returned by controller                                                                         
                                                });                                         
                            }
                    });             
        });
    </script>
</head>

<body>
<h1>Uploadify Example</h1>

    <?php echo form_open_multipart('upload/index');?>

    <p>
        <label for="Filedata">Choose a File</label><br/>
        <?php echo form_upload(array('name' => 'Filedata', 'id' => 'upload'));?>
        <a href="javascript:$('#upload').uploadifyUpload();">Upload File(s)</a>
    </p>


    <?php echo form_close();?>

    <div id="target">

    </div>
</body>
</html>

my view : uploadify 我的看法:uploadify

<html>
    <ul>

        <li>Extension: <?php echo $json->{'file_ext'};?></li>
        <li>File Size: <?php echo $json->{'file_size'};?></li>
        <li>File Path: <?php echo $json->{'file_path'};?></li>
    </ul>
    </html>

and then parsing that json_array variable to my view, that is the plans, but in reality that code doesn work the data is undefined an error Trying to get property of non-object i use this code here , I suppose the problem is with json 然后解析该json_array变量到我的视图,这就是计划,但实际上代码无法正常工作,数据是未定义的错误。尝试获取非对象的属性,我在这里使用此代码,我想问题出在json

i just want to use the data file uploaded if anyone can solve that problem please share it or send me CI+uploadify program to my email, if anyone expert about CI and Uploadify plugin please make the tutorial step by step how to use it, step by step, i think it would be great help for newbie like me 我只想使用上传的数据文件,如果有人可以解决该问题,请共享它或将CI + upify程序发送给我,如果我有关于CI和Uploadify插件的专家,请逐步完成该教程的使用方法一步一步,我认为这对像我这样的新手将有很大的帮助

thanks.... 谢谢....

my email :saya.dean@gmail.com 我的电子邮件:saya.dean@gmail.com

I'm not really clear on where you are running into a problem. 我不清楚您在哪里遇到问题。 'that variable' will be the files you uploaded, yes? “那个变量”将是您上传的文件,是吗? Create an array of the filepaths as they get uploaded and when the upload is done cycle through each for email attachment. 创建一个文件路径数组,这些文件路径将在上传时以及上传完成后在每个电子邮件附件中循环循环。 Have you checked other answers on the site? 您是否在网站上检查了其他答案? Maybe take a look here . 也许在这里看看。 But CI's documentation clearly states that you can use: 但是CI的文档明确指出您可以使用:

$this->email->attach('/path/to/that_file.jpg');

multiple times. 多次。

Update: 更新:

You can either try using the onUploadSuccess function in uploadify to append each file name to something that you can use later... 您可以尝试在uploadify中使用onUploadSuccess函数,将每个文件名附加到稍后可以使用的名称上。

  'onUploadSuccess' : function(file, data, response) {
     alert('The file name is ' + file.name);
     ...

OR from within uploadify.php. 或从uploadify.php中。 From there you can store what you need for attaching after. 从那里您可以存储以后需要附加的内容。

In your case I'd stick with modifying the uploadify.php . 在您的情况下,我会坚持修改uploadify.php You'll have to give it a shot and post some code if you are stuck, but there are plenty of places to get some ideas like here and here 如果遇到困难,您必须先尝试一下并发布一些代码,但是有很多地方可以在这里这里获得一些想法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM