uploadify上传后预览Demo

来源:互联网 发布:windows密钥能用几次 编辑:程序博客网 时间:2024/06/05 19:43

V层

<!doctype html><html><head><meta charset="utf-8"><title>无标题文档</title><script type="text/javascript" src="<?php echo base_url('type/js')?>/jquery.min.js"></script><script src="<?php echo base_url('type/js')?>/jquery.uploadify.min.js" type="text/javascript"></script><link rel="stylesheet" type="text/css" href="<?php echo base_url('type/css')?>/uploadify.css"></head><body>    <input id="file_upload" name="file_upload" type="file" >    <script type="text/javascript">        var url = "<?php echo site_url('index/upload_file')?>";        $('#file_upload').uploadify({            swf:'./type/swf/uploadify.swf',//定义uploadify.swf的路径。            uploader:url,//定义服务器端上传数据处理脚本文件uploadify.php的路径。            fileTypeDesc:'Image File',//可选择的文件类型的描述。此字符串出现在浏览文件对话框的文件类型下拉菜单中。            fileTypeExts:'*.jpeg;*.jpg;*.png',//定义允许上传的文件后缀。            buttonText:'上传',//定义显示在默认按钮上的文本。            fileSizeLimit:1024,//上传文件大小限制。               multi:false,//设置值为false时,一次只能选中一个文件。            auto:true,//设置auto为true,当文件被添加至上传队列时,将会自动上传。            removeTimeout:0,            onUploadSuccess:function(file,data,response){//每一个文件上传成功时触发该事件。                //$('#img_show').html('<img src="'+data+'" width="200" height="250"  />');            }        });    </script></body></html>

C层

//uploadify原版function upload_file(  ){    $f = 'ap_'.time().'_'.strtoupper(substr(md5(rand()),0,4)).'.png';    $ymd = date('Ym',time());    $targetFolder = "./uploads/$ymd/";    if ( !file_exists( $targetFolder ) ) {        mkdir( $targetFolder );    }     if (!empty($_FILES) ) {        $tempFile = $_FILES['Filedata']['tmp_name'];        $targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;        $targetFile = rtrim($targetPath,'/') . '/' . $f;        // Validate the file type        $fileTypes = array('jpg','jpeg','gif','png'); // File extensions        $fileParts = pathinfo($_FILES['Filedata']['name']);        if (in_array($fileParts['extension'],$fileTypes)) {            move_uploaded_file($tempFile,$targetFile);            echo base_url("uploads/$ymd").'/'.$f;        } else {            echo 'Invalid file type.';        }    }                   }
//ci版本function upload_file(  ){    if (!empty($_FILES['Filedata']['tmp_name'])){    $this->load->library('upload');    $file_name = 'ap_'.time().'_'.strtoupper(substr(md5(rand()),0,4));    $ymd = date('Ymd',time());    $upload_path = "./uploads/$ymd/";    if ( !file_exists( $upload_path ) ) {        mkdir( $upload_path );    }     $config['file_name'] = $file_name;    $config['upload_path'] = $upload_path;    $config['allowed_types'] = 'gif|jpeg|jpg|png';    $config['overwrite'] = TRUE;    $config['detect_mime'] = TRUE;    $config['mod_mime_fix'] = TRUE;    $config['max_size'] = '1024';    //$config['max_width'] = '1024';    //$config['max_height'] = '768';    $this->upload->initialize($config);    if ( ! $this->upload->do_upload('Filedata')){            echo '上传失败';//上传失败        }else{            echo base_url("uploads/$ymd").'/'.$this->upload->data()['file_name'];//上传成功,返回文件名,包含后缀            }    }else{        echo '上传失败';//上传失败    } }
1 0
原创粉丝点击