• 网页上查看上传图片


    1.在form中,必须有 enctype 提交方法必须是POST

     <form class="formAjax" enctype="multipart/form-data" method="POST" id="modelform">

    2.上传图片,可以在网页中查看

                    <tr rowspan='4'>
                        <?php if ($url_method =='create'): ?>
                            <img src="/images/photo-empty.jpg" alt=""
                                 style="110px;height:139px;float:left;padding-left:210px;" id="photopath">
                        <?php else: ?>
                            <img src="" alt=""
                                 style="110px;height:139px;float:left;padding-left:210px;" id="photopath">
                        <?php endif; ?>
                    </tr>
                    <tr>
                        <p class="short-input ue-clear">
                            <input type="file" name="addfile" id="proimg" style="position: relative;left: 145px;"
                           onchange="Preview(this,'photopath','divphotopath');">
                        </p>
                    </tr>

         js代码

     function Preview(obj, obj1, obj2) {
            if (document.all) {
                $("#" + obj1).css("width", "0px");
                $("#" + obj1).css("height", "0px");
                $("#" + obj2).css("display", "block");
                document.getElementById('' + obj2 + '').filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = obj.value;
            } else {
                $('#' + obj1).show();
                var file = obj, objectURL = window.URL.createObjectURL(file.files[0]);
                $('#' + obj1).attr("src", objectURL);
            }
        }

    3.不能用ajax传递,必须用ajaxSubmit进行传递

     $("#modelform").ajaxSubmit({
    })

     4.php后端代码

                $config['upload_path'] = FCPATH . 'uploads/userphoto/';
                $config['allowed_types'] = 'png|jpg|jpeg';
                $config['max_size'] = '2000000';
                $config['overwrite'] = FALSE;
                $config['encrypt_name'] = TRUE;
                $CI =& get_instance();
                $CI->load->library('upload', $config);
                if ($_FILES['addfile']['name'] != '') {
                    if ($CI->upload->do_upload('addfile')) {
                        $str = $CI->upload->data();
                        $file = realpath(FCPATH . 'uploads/userphoto/');
                        if (!is_dir($file)) mkdir($file);
                        $filename = $datainfo[$this->mydb->getKeyId()] . date("YmdHis") . $str['file_ext'];
                        $file = $file . '/' . $filename;
                        if (is_file($file)) unlink($file);
                        copy($str['full_path'], $file);
                        unlink($str['full_path']);
                        $file = "http://" . $_SERVER["HTTP_HOST"] . '/uploads/userphoto/' . $filename;
                        $datainfo ['photopath'] = $file;
                    } else {
                        $result = Ousuclass::getJsonResult();
                        $result['success'] = FALSE;
                        $result['msg'] = '上传文件错误!';
                        echo json_encode($result);
                        exit;
                    }
                }
  • 相关阅读:
    蓝桥杯--2012--奇怪的比赛(全排列)
    mysql数据库读写分离教程
    Linux中安装java JDK
    mysql数据库主从复制教程
    mysql数据库安装教程
    http报文详解
    查看磁盘I/O命令iostat详解
    linux如何查看服务器当前的并发访问量
    Another app is currently holding the yum lock; waiting for it to exit...
    linux清理缓存cache
  • 原文地址:https://www.cnblogs.com/lqw4/p/4929528.html
Copyright © 2020-2023  润新知