php頭像上傳預(yù)覽實(shí)例代碼
說道上傳圖片,大家并不陌生,不過,在以后開發(fā)的項(xiàng)目中,可能并不會讓你使用提交刷新頁面式的上傳圖片,比如上傳頭像,按照常理,肯定是在相冊選擇照片之后,確認(rèn)上傳,而肯定不會通過form表單,點(diǎn)擊submit刷新式上傳。我為大家介紹兩種異步非刷新式上傳圖片+圖片預(yù)覽:第一種,通過現(xiàn)成的uploadfy插件進(jìn)行上傳,網(wǎng)上好多實(shí)例。
不過我重點(diǎn)為大家介紹的是第二種,通過Ajax上傳圖片。因?yàn)槭褂胾ploadfy插件需要設(shè)備支持swf格式的Flash,所以對大多數(shù)手機(jī)來說,第一種方式就沒辦法使用了。首先,我先跟大家說一下上傳原理:通過js控制file文本域,當(dāng)選擇照片之后,通過Ajax異步提交form表單,然后將圖片的位置作為返回值,使用js把img的src屬性設(shè)置為返回值。
上傳頭像區(qū)域:
代碼:
<!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>無標(biāo)題文檔</title>
<link href="bootstrap-3.3.7-dist/css/bootstrap.min.css" rel="external nofollow" 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>
<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">關(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>
上傳的處理頁:
<?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>";
}
}
}
原理:
通過form表單的enctype="multipart/form-data"屬性將文件臨時放到wamp文件夾中的tmp目錄下,再通過后臺php程序?qū)⑽募4嬖隗w統(tǒng)中。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
WordPress中設(shè)置Post Type自定義文章類型的實(shí)例教程
這篇文章主要介紹了WordPress中設(shè)置Post Type自定義文章類型的實(shí)例教程,后臺文章類型的設(shè)置是WordPress的一大特色,然而自帶的文章類型往往并不夠用,需要的朋友可以參考下2016-05-05
Thinkphp5.0 框架視圖view的比較標(biāo)簽用法分析
這篇文章主要介紹了Thinkphp5.0 框架視圖view的比較標(biāo)簽用法,結(jié)合實(shí)例形式分析了thinkPHP5框架eq、equal、neq、notequal、egt及switch、range、between等標(biāo)簽相關(guān)用法,需要的朋友可以參考下2019-10-10
yii實(shí)現(xiàn)CheckBox復(fù)選框在同一行顯示的方法
這篇文章主要介紹了yii實(shí)現(xiàn)CheckBox復(fù)選框在同一行顯示的方法,對比了網(wǎng)上搜集的方法,給出了改進(jìn)的意見,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-12-12
PHP 實(shí)現(xiàn)手機(jī)端APP支付寶支付功能
最近應(yīng)業(yè)務(wù)需求,做了支付寶支付和微信支付,今天分享一下手機(jī)端app支付寶支付對接流程,感興趣的朋友跟隨腳本之家小編一起看看吧2018-06-06
PHP長網(wǎng)址與短網(wǎng)址的實(shí)現(xiàn)方法
這篇文章主要介紹了PHP長網(wǎng)址與短網(wǎng)址的實(shí)現(xiàn)方法,需要的朋友可以參考下2017-10-10
利用php+mysql來做一個功能強(qiáng)大的在線計(jì)算器
有天在努力的搜索計(jì)算器,發(fā)現(xiàn)都是JavaScript,而且要一個個地點(diǎn)擊,并且不能記錄,輸入計(jì)算式子時容易出錯,于是就想了想該怎樣才能讓它好用點(diǎn)呢,能夠用鍵盤直接輸入。2010-10-10

