php實現頭像上傳預覽功能的執行個體

來源:互聯網
上載者:User
php頭像上傳帶預覽:

說道上傳圖片,大家並不陌生,不過,在以後開發的項目中,可能並不會讓你使用提交重新整理頁面式的上傳圖片,比如上傳頭像,按照常理,肯定是在相簿選擇照片之後,確認上傳,而肯定不會通過form表單,點擊submit重新整理式上傳。我為大家介紹兩種非同步非重新整理式上傳圖片+圖片預覽:第一種,通過現成的uploadfy外掛程式進行上傳,網上好多執行個體。不過我重點為大家介紹的是第二種,通過Ajax上傳圖片。因為使用uploadfy外掛程式需要裝置支援swf格式的Flash,所以對大多數手機來說,第一種方式就沒辦法使用了。首先,我先跟大家說一下上傳原理:通過js控制file文本域,當選擇照片之後,通過Ajax非同步提交form表單,然後將圖
片的位置作為傳回值,使用js把img的src屬性設定為傳回值。

上傳頭像地區:

代碼:

<!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><!-- 按鈕觸發模態框 --><button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">    上傳頭像</button><!-- 模態框(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">                    &times;                </button>                <h4 class="modal-title" id="myModalLabel">                    上傳頭像                </h4>            </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">關閉                </button>                <!--<button type="button" class="btn btn-primary">                    提交更改                </button>-->                            </div>        </div><!-- /.modal-content -->    </div><!-- /.modal --></div></body><script type="text/javascript">//回呼函數,調用該方法傳一個檔案路徑,該變背景圖function showimg(url){    var div = document.getElementById("yl");    div.style.backgroundImage = "url("+url+")";        document.getElementById("tp").value = url;}</script></html>

上傳的處理頁:

<?phpif($_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>";        }            }}

原理:

通過form表單的enctype="multipart/form-data"屬性將檔案臨時放到wamp檔案夾中的tmp目錄下,再通過後台php程式將檔案儲存在體統中。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.