溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

php怎么實現(xiàn)頭像上傳

發(fā)布時間:2021-10-15 16:42:10 來源:億速云 閱讀:269 作者:小新 欄目:編程語言

這篇文章主要為大家展示了“php怎么實現(xiàn)頭像上傳”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“php怎么實現(xiàn)頭像上傳”這篇文章吧。

php實現(xiàn)頭像上傳的方法:【<?php if($_FILES["file"]["error"]){echo $_FILES["file"]["error"];}else{if(($_FILES["file"]["type"]=="...】。

本文操作環(huán)境:windows10系統(tǒng)、php 7、thinkpad t480電腦。

最近在做項目時需要使用到頭像上傳的功能,于是記錄下實現(xiàn)的過程。

如果我們要完成頭像上傳功能需要php頁面,一個頁面我們定義為touxiang.php,另一個頁面我們定義為upload.php。

看下具體代碼:

1.touxiang.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>

<link href="bootstrap-3.3.7-dist/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="bootstrap-3.3.7-dist/js/jquery-1.11.2.min.js"></script>
<script src="bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>

<style type="text/css">
#yl{ width:200px; height:200px; background-image:url(img/avatar.png); background-size:200px 200px;}
#file{ width:200px; height:200px; float:left; opacity:0;}
.modal-content{ width:500px;}
.kk{ margin-left:130px;}
</style>

</head>

<body>



<!-- 按鈕觸發(fā)模態(tài)框 -->
<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
    上傳頭像
</button>
<!-- 模態(tài)框(Modal) -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                    ×
                </button>
                <h5 class="modal-title" id="myModalLabel">
                    上傳頭像
                </h5>
            </div>
            <div class="modal-body">
                <form id="sc" action="upload.php" method="post" enctype="multipart/form-data" target="shangchuan">

    <input type="hidden" name="tp" value="" id="tp" />

    <div id="yl" class="kk">
        <input type="file" name="file" id="file" onchange="document.getElementById('sc').submit()" />
    </div>



</form>

<iframe style="display:none" name="shangchuan" id="shangchuan">
</iframe>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">關(guān)閉
                </button>
                <!--<button type="button" class="btn btn-primary">
                    提交更改
                </button>-->

            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>


</body>

<script type="text/javascript">

//回調(diào)函數(shù),調(diào)用該方法傳一個文件路徑,該變背景圖
function showimg(url)
{
    var div = document.getElementById("yl");
    div.style.backgroundImage = "url("+url+")";

    document.getElementById("tp").value = url;
}

</script>

</html>

在這個頁面我們需要引入一個模態(tài)框和bootstrap.min.css,jquery-1.11.2.min.js,bootstrap.min.js三個文件

2.upload.php

<?php

if($_FILES["file"]["error"])
{
    echo $_FILES["file"]["error"];
}
else
{
    if(($_FILES["file"]["type"]=="image/jpeg" || $_FILES["file"]["type"]=="image/png")&& $_FILES["file"]["size"]<1024000000)
    {
        $fname = "./img/".date("YmdHis").$_FILES["file"]["name"];

        $filename = iconv("UTF-8","gb2312",$fname);

        if(file_exists($filename))
        {
            echo "<script>alert('該文件已存在!');</script>";
        }
        else
        {
            move_uploaded_file($_FILES["file"]["tmp_name"],$filename);

            unlink($_POST["tp"]);

            echo "<script>parent.showimg('{$fname}');</script>";
        }

    }
}

效果如下所示:

php怎么實現(xiàn)頭像上傳

以上是“php怎么實現(xiàn)頭像上傳”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI