首页 >后端开发 >PHP问题 >php如何预览图片

php如何预览图片

(*-*)浩
(*-*)浩原创
2019-09-29 11:42:223594浏览

php如何预览图片

PHP代码(推荐学习:PHP视频教程

<?php
header("Content-type:text/html;charset=utf-8");

$upFile = $_FILES[&#39;file&#39;];


/**
* 创建文件夹函数,用于创建保存文件的文件夹
* @param str $dirPath 文件夹名称
* @return str $dirPath 文件夹名称
*/
    function creaDir($dirPath){
        $curPath = dirname(__FILE__);
        $date = date(&#39;Y-m-d&#39;,time());
        $path = $curPath.&#39;\\&#39;.$dirPath.&#39;\\&#39;.$date;

        if (is_dir($path) || mkdir($path,0777,true)) {
            return $dirPath;
        }
    }

    //判断文件是否为空或者出错
    if ($upFile[&#39;error&#39;]==0 && !empty($upFile)) {
        $dirpath = creaDir(&#39;upload1&#39;);
        $filename = iconv("utf-8","gbk",$_FILES[&#39;file&#39;][&#39;name&#39;]);
        $date = date(&#39;Y-m-d&#39;,time());
        $queryPath = &#39;./&#39;.$dirpath.&#39;/&#39;.$date.&#39;/&#39;.$filename;

        // 判断上传的是不是图片
        if($_FILES[&#39;file&#39;][&#39;type&#39;] == &#39;image/jpeg&#39; || $_FILES[&#39;file&#39;][&#39;type&#39;]==&#39;image/jpg&#39;){
            //move_uploaded_file将浏览器缓存file转移到服务器文件夹
            if(move_uploaded_file($_FILES[&#39;file&#39;][&#39;tmp_name&#39;],$queryPath)){
                $filename = iconv("utf-8","utf-8",$_FILES[&#39;file&#39;][&#39;name&#39;]);
                $queryPath = &#39;./&#39;.$dirpath.&#39;/&#39;.$date.&#39;/&#39;.$filename;
                echo $queryPath;
            }
        }
    }

?>

解决上传的图片文件名中文乱码:

$filename = iconv("utf-8","gbk",$_FILES[&#39;file&#39;][&#39;name&#39;]);

解决无法预览的问题:

$filename = iconv("utf-8","utf-8",$_FILES[&#39;file&#39;][&#39;name&#39;]);

html代码

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>上传图片</title>
    <script src="./js/jquery-1.10.2.min.js"></script>
    <script type="text/javascript">
        $(function(){
            $(&#39;div&#39;).css(&#39;color&#39;,&#39;red&#39;);
            $("#up").click(function() {
            //formdata储存异步上传数据
                var formData = new FormData($(&#39;form&#39;)[0]);
                formData.append(&#39;file&#39;,$(&#39;:file&#39;)[0].files[0]);
                //坑点: 无论怎么传数据,console.log(formData)都会显示为空,但其实值是存在的,f12查看Net tab可以看到数据被上传了0
                $.ajax({
                    url:&#39;./upload.php&#39;,
                    type: &#39;POST&#39;,
                    data: formData,
                    //这两个设置项必填
                    contentType: false,
                    processData: false,
                    success:function(data){
                        // console.log(data)
                        var srcPath = data;
                        //console.log(111);
                    //注意这里的路径要根据自己的储存文件的路径设置
                        $(&#39;.picDis img&#39;).attr(&#39;src&#39;,srcPath);
                    }
                })
            });
        });


    </script>
</head>
<body>
    <form enctype="multipart/form-data" id="upForm">
        <input type="file" name="file" ><br><br>
        <input type="button" value="提交" id="up">
    </form>
    <div class="picDis">
        <img src="" alt="">
    </div>

</body>
</html>

以上是php如何预览图片的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn