- 相關(guān)推薦
PHP+jQuery+Ajax仿淘寶多上傳按鈕單文件上傳
如何做一個(gè)仿淘寶多上傳的按鈕單文件上傳呢?下面是由百分網(wǎng)小編為大家整理的PHP+jQuery+Ajax仿淘寶多上傳按鈕單文件上傳,喜歡的可以收藏一下!了解更多詳情資訊,請(qǐng)關(guān)注應(yīng)屆畢業(yè)生考試網(wǎng)!
其代碼如下:
上傳表單
<form class="imageform" method="post" enctype="multipart/form-data" action="upload.php">
<div class="up_status" style="display:none"><img src="loader.gif" alt="uploading"/></div>
<div class="btn up_btn">
<span>添加圖片</span>
<input class="photoimg" type="file" name="photoimg">
</div>
</form>
<div class="preview_img"></div>
引入樣式和上傳插件jquery.wallform.js
<link rel="stylesheet" type="text/css" href="css/style.css" />
<script type="text/javascript" src="http://libs.useso.com/js/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="jquery.wallform.js"></script>
jQuery
$("body").on("change", ".photoimg",
function() {
var obj = $(this);
var imageForm = obj.parents(".imageform");
var preview_img = imageForm.next(".preview_img");
var btn = imageForm.find(".up_btn");
imageForm.ajaxForm({
target: preview_img,
beforeSubmit: function() {
imageForm.next("div.preview_img").html("");
preview_img.hide();
btn.hide();
},
success: function() {
preview_img.show();
btn.show();
},
error: function() {
btn.show();
preview_img.hide();
}
}).submit();
});
PHP上傳 upload.php
if (isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST") {
$name = $_FILES['photoimg']['name'];
$size = $_FILES['photoimg']['size'];
if (empty($name)) {
echo '請(qǐng)選擇要上傳的圖片';
exit;
}
$ext = extend($name);
if (!in_array($ext, $extArr)) {
echo '圖片格式錯(cuò)誤!';
exit;
}
if ($size > (1000 * 1024)) {
echo '圖片大小不能超過(guò)1M';
exit;
}
$image_name = time() . rand(100, 999) . "." . $ext;
$tmp = $_FILES['photoimg']['tmp_name'];
if (move_uploaded_file($tmp, $path . $image_name)) {
echo '<img src="' . $path . $image_name . '" class="preview">';
} else {
echo '上傳出錯(cuò)了!';
}
exit;
}
【PHP+jQuery+Ajax仿淘寶多上傳按鈕單文件上傳】相關(guān)文章:
PHP文件上傳源碼分析10-13
php使用html5實(shí)現(xiàn)多文件上傳實(shí)例09-04
jQuery Mobile + PHP實(shí)現(xiàn)文件上傳10-20
java上傳文件到七牛云中09-23
php多圖片上傳的簡(jiǎn)單例子10-09