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

來源:互聯網
上載者:User
這篇文章主要為大家詳細介紹了php實現頭像上傳預覽功能的相關資料,具有一定的參考價值,感興趣的小夥伴們可以參考一下

首頁面1.php


<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>上傳頭像</title><style type="text/css">  *{    font-family:"微軟雅黑";}  #zong{    /*border:1px solid black;*/    position:relative;    width:52%;    height:500x;    left:24%}  .nr{    float:left;    margin-right:30px;}  #yl{width:240px; height:240px; background-size:240px 240px;}  #file{width:240px; height:240px; float:left; opacity:0;}</style></head><body><p id="zong"><form id="sc" action="2.php" method="post" enctype="multipart/form-data" target="shangchuan">    <input type="hidden" name="tp" value="" id="tp" />    <p id="yl" style="background-image:url(./image/1.jpg)" class="nr">//頭像顯示的位置    <input type="file" name="file" id="file" onchange="document.getElementById('sc').submit()" />  </p>  <p class="nr">  </p> </form><iframe style="display:none" name="shangchuan" id="shangchuan"></iframe></p></body><script type="text/javascript">//回呼函數,調用該方法傳一個檔案路徑,改變背景圖function showimg(url){  var p = document.getElementById("yl");  p.style.backgroundImage = "url("+url+")";    document.getElementById("tp").value = url;}</script></html>

處理頁面2.php


<?phpsession_start();$uid = $_SESSION["uid"];if($_FILES["file"]["error"]){  echo $_FILES["file"]["error"];}else{  if(($_FILES["file"]["type"]=="image/jpeg" || $_FILES["file"]["type"]=="image/png")&& $_FILES["file"]["size"]<1024000)  {    $fname = "./a/image/".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>";    }      }}

開啟顯示:

點擊圖片位置彈出選擇框:

選擇圖片完成後:

以上就是本文的全部內容,希望對大家的學習有所協助。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.