Home  >  Article  >  Backend Development  >  How to preview pictures in php

How to preview pictures in php

(*-*)浩
(*-*)浩Original
2019-09-29 11:42:223514browse

How to preview pictures in 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>

The above is the detailed content of How to preview pictures in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn