php上传cvs文件完整例子
下面给大家整理了个php上传cvs文件完整例子,希望此例子对各位同学会有所帮助自己没注意看不知道什么原因要上传csv文件.
html代码:
- <form action="/message/index/csv" encType="multipart/form-data" method="post" target="uploadcvs">
- <div><input type="file" name="csvfile"/> <input type="submit" value="上传"></div>
- <div id="upload_info" style="width:100%;border:1px solid #809db9;display:none" ></div>
- </form>
target="uploadcvs",添加target时调试代码不好调试,建议调试时去掉这行代码.
php代码:
- public function csvAction(){
-
- $fileinfo = pathinfo($_FILES['csvfile']['name']);
-
- if(!in_array(strtolower($fileinfo['extension']),array('csv'))){
- $feed = array('status'=>'ext');
-
- }else if($_FILES['csvfile']['size'] > 1*1024*1024){
- $feed = array('status'=>'size');
- }else{
-
- $succdata = array();
-
- $errdata = array();
-
- $count = 0;
-
- $handle = fopen($_FILES['csvfile']['tmp_name'],"r");
-
-
- $userMongo = new Application_Model_DbTable_MongoUsers();
- $dbUsers = $userMongo->findAll(array(),array('login.username'=>true));
-
- $users = array();
- foreach($dbUsers as $user){
- if(isset($user['login']['username']))
- $users[] = $user['login']['username'];
- }
-
- while ($row = fgetcsv($handle)) {
-
- $count ++ ;
- foreach($users as $user){
-
-
- if(preg_match('/^1(3|4|5|8)d{9}$/',$row[0]) && $user==$row[0]){
- $succdata[$row[0]] = (string)$row[0];
- break;
- }
- }
-
- if(!isset($succdata[$row[0]])){
- $errdata[$row[0]] = $row[0];
- }
- }
-
- fclose($handle);
-
- $url = $this->_helper->Upload->msgcsv($_FILES['csvfile'], $succdata, $errdata);
-
- $count = array('original'=>$count, 'success'=>count($succdata), 'error'=>count($errdata));
-
-
- $feed = array('status'=>'succ','count'=>$count, 'url'=>$url);
- }
-
- $feed = json_encode($feed);
-
- echo "<script>parent.callback(".$feed.")</script>";
- exit;
- }
js代码如下:
-
- function callback(data){
- try{
- if(data.status =='ext'){
- alert('文件类型不正确');
- return false;
- }else if(data.status == 'size'){
- alert('文件不能超过1M');
- return false;
- }else if(data.status == 'succ'){
-
- var html = parseInt(data.count.error) > 0 ? ' <a href="<?php echo UPLOAD_IMAGE_PATH;?>'+data.url.error+'">查看失败列表</a>' : '';
-
- $('#upload_info').html('成功导入:'+data.count.success+';失败:'+data.count.error+html);
- $('#upload_info').show();
- $('input[name=step3_hidden]').val(data.url.success);
- }
- }catch(e){
- alert('解析出错,请检查文件格式.');
- }
- return false;
- }
Upload.php文件代码如下:
- <?php
-
- class Ata_Controller_Action_Helper_Upload extends Zend_Controller_Action_Helper_Abstract {
-
-
- public function MsgCsv(&$source, $succdata, $errdata){
-
-
-
- $publicPath = realpath(APPLICATION_PATH."/../public/upload").'/';
- $csvPath = "msgcsv/".date('Y').'/'.date('m')."/";
- $path = $publicPath.$csvPath;
-
- $this->mkdirs($path);
-
- $filename = uniqid();
-
- $ext = pathinfo($source['name'], PATHINFO_EXTENSION);
-
- $filepath = $path.$filename.'_org.'.$ext;
-
- move_uploaded_file($source['tmp_name'], $filepath);
-
-
- $content = '';
-
- $succfile = $path.$filename.'_succ.'.$ext;
-
- foreach($succdata as $row){
-
- $content .= $row."n";
- }
- $content = trim($content,"n");
-
- file_put_contents($succfile, $content);
-
-
- $content = '';
- $errfile = $path.$filename.'_err.'.$ext;
- foreach($errdata as $row){
- $content .= $row."n";
- }
- $content = trim($content,"n");
- file_put_contents($errfile, $content);
-
-
- return array(
- 'orginal' => $csvPath.$filename.'_org.'.$ext,
- 'success' => $csvPath.$filename.'_succ.'.$ext,
- 'error' => $csvPath.$filename.'_err.'.$ext
- );
- }
-
-
- public function rmMsgCsv($path){
-
- $prefix = str_replace('_succ.csv','',$path);
-
- unlink($prefix.'_org.csv');
- unlink($prefix.'_succ.csv');
- unlink($prefix.'_err.csv');
- }
-
- public function mkdirs($dir, $mode = 0777) {
- return is_dir($dir) or ($this->mkdirs(dirname($dir)) and mkdir($dir, $mode));
- }
- }