php - attach a file from directory and send to email with codeigniter -
i newbie codeigniter , want build website using codeigniter framework. first, fine can utilize database, validation, email, session , seek attach file , send email :
$this->email->attach('/path/ofyour/constan/file.anything');
thats work too.
since website want client take file want upload. seek many method, , many of them tell upload file server root , file_data, utilize file_data[file_patch]
$this->email->attach('file_data[file_path]');
the problem is:
since code igniter cant upload multiple info must utilize plugin. tried , pain
i thing not effective, upload info server root , email?
it improve file_path of upload file , send them email, how?
i build jquery mobile, must do?
updateok decide utilize uploadify search every website , found here , code is
uploadify.php
<?php /* * functions taken 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)) { homecoming $filename; } $filename = str_replace($file_ext, '', $filename); $new_filename = ''; ($i = 1; $i < 100; $i++) { if ( ! file_exists($path.$filename.$i.$file_ext)) { $new_filename = $filename.$i.$file_ext; break; } } if ($new_filename == '') { homecoming false; } else { homecoming $new_filename; } } function prep_filename($filename) { if (strpos($filename, '.') === false) { homecoming $filename; } $parts = explode('.', $filename); $ext = array_pop($parts); $filename = array_shift($parts); foreach ($parts $part) { $filename .= '.'.$part; } $filename .= '.'.$ext; homecoming $filename; } function get_extension($filename) { $x = explode('.', $filename); homecoming '.'.end($x); } // uploadify v1.6.2 // copyright (c) 2009 ronnie garcia // co-developed 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 going on here, said newbie know $json_array, array hold info $filearray, info file uploaded. mission 1 complete
now controller: 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 /js/uploadify/upload.php */ function uploadify() { //decode json returned /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 send info in oncomplete function
my view :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 , 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 controller $.post('<?php echo site_url('upload/uploadify');?>',{filearray: response},function(info){ $("#target").append(info); //add response returned controller }); } }); }); </script> </head> <body> <h1>uploadify example</h1> <?php echo form_open_multipart('upload/index');?> <p> <label for="filedata">choose 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
<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 parsing json_array variable view, plans, in reality code doesn work info undefined error trying property of non-object utilize code here, suppose problem json
i want utilize info file uploaded if can solve problem please share or send me ci+uploadify programme email, if expert ci , uploadify plugin please create tutorial step step how utilize it, step step, think great help newbie me
thanks....
my email :saya.dean@gmail.com
i'm not clear on running problem. 'that variable' files uploaded, yes? create array of filepaths uploaded , when upload done cycle through each email attachment. have checked other answers on site? maybe take here. ci's documentation states can use:
$this->email->attach('/path/to/that_file.jpg');
multiple times.
update:you can either seek using onuploadsuccess function in uploadify append each file name can utilize later...
'onuploadsuccess' : function(file, data, response) { alert('the file name ' + file.name); ...
or within uploadify.php. there can store need attaching after.
in case i'd stick modifying uploadify.php. you'll have give shot , post code if stuck, there plenty of places ideas here , here
php file-upload codeigniter-2 uploadify email-attachments
No comments:
Post a Comment