PHP AjaxForm提交圖片上傳并顯示圖片源碼
本文實(shí)例為大家分享了PHP AjaxForm提交圖片上傳并顯示圖片的具體代碼,供大家參考,具體內(nèi)容如下
PHP dofile.php 文件上傳源碼
<?php
$file_upload = "upload/";
$file_allow_ext='gif|jpg|jpeg|png|gif|zip|rar|ppt|xls|pdf|pptx|xlsx|docx';
$file_allow_size = 5*1024*1024;
if($_POST['submit']=="上傳"){
if(is_uploaded_file($_FILES['file']['tmp_name'])){
$file_name = $_FILES['file']['name'];
$file_error = $_FILES['file']['error'];
$file_type = $_FILES['file']['type'];
$file_tmp_name = $_FILES['file']['tmp_name'];
$file_size = $_FILES['file']['size'];
$file_ext = substr($file_name, strrpos($file_name, '.')+1);
switch($file_error){
case 0:
$data['status'] = 0;
$data['msg'] = "文件上傳成功!";
break;
case 1:
$data['status'] = 1;
$data['msg'] = "文件上傳失敗,文件大小".$file_size."超過(guò)限制,允許上傳大小".sizeFormat($file_allow_size)."!";
break;
case 3:
$data['status'] = 1;
$data['msg'] = "上傳失敗,文件只有部份上傳!";
break;
case 4:
$data['status'] = 1;
$data['msg'] = "上傳失敗,文件沒(méi)有被上傳!";
break;
case 5:
$data['status'] = 1;
$data['msg'] = "文件上傳失敗,文件大小為0!";
break;
}
if(stripos($file_allow_ext,$file_ext)===false){
$data['status'] = 1;
$data['msg'] = "該文件擴(kuò)展名不允許上傳";
}
if($file_size>$file_allow_size){
$data['status'] = 1;
$data['msg'] = "文件大小超過(guò)限制,只能上傳".sizeFormat($file_allow_size)."的文件!";
}
if($data['status']==1){
$data['status'] = 1;
$data['msg'] = $data['msg'];
exit(json_encode($data));
}
if($data['status']==0){
if(file_exists($file_upload)){
$file_new_name = date("YmdHis").'_'.rand(10000,99999).'.'.$file_ext;
$file_save_path = $file_upload.$file_new_name;
$data['status'] = 0;
$data['url'] = $file_save_path;
move_uploaded_file($file_tmp_name,$file_save_path);
exit(json_encode($data));
}else{
exit(json_encode($data));
}
}
}
}
function sizeFormat($size)
{
$sizeStr='';
if($size<1024)
{
return $size."bytes";
}
else if($size<(1024*1024))
{
$size=round($size/1024,1);
return $size."KB";
}
else if($size<(1024*1024*1024))
{
$size=round($size/(1024*1024),1);
return $size."MB";
}
else
{
$size=round($size/(1024*1024*1024),1);
return $size."GB";
}
}
?>
HTML如下
<script type="text/javascript" src="http://lib.sinaapp.com/js/jquery/1.9.1/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="http://files.cnblogs.com/files/china-li/jquery.form.js"></script>
<form action="dofile.php" method="post" enctype="multipart/form-data" id="upfileimage">
<input type="hidden" name="image[]" />
<label for="file">文件:</label><input type="file" name="file" id="file" />
<input type="submit" name="submit" value="上傳" />
</form>
<script type="text/javascript">
$("#upfileimage").submit(function(){
if($("input[type=file]").val()==""){
alert("請(qǐng)選擇要上傳的文件");
return false;
}
})
$(function(){
var options = {
type:"POST",
dataType:"json",
resetForm:true,
success:function(o){
if(o.status==1){
alert(o.msg);
}else{
$("body").append(" <img src='"+o.url+"' alt='' width='100' /><input type='hidden' name='image[]' value='"+o.url+"' />");
}
},
error:function(o){
alert(o.message);
}
}
$("#upfileimage").ajaxForm(options).submit(function(){return false;});
})
</script>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- PHP+jQuery+Ajax實(shí)現(xiàn)多圖片上傳效果
- jquery+php+ajax顯示上傳進(jìn)度的多圖片上傳并生成縮略圖代碼
- php+ajax實(shí)現(xiàn)圖片文件上傳功能實(shí)例
- php ajax無(wú)刷新上傳圖片實(shí)例代碼
- php+ajax實(shí)現(xiàn)異步上傳文件或圖片功能
- php+html5+ajax實(shí)現(xiàn)上傳圖片的方法
- PHP+Ajax無(wú)刷新帶進(jìn)度條圖片上傳示例
- php+ajax無(wú)刷新上傳圖片實(shí)例代碼
- PHP中ajax無(wú)刷新上傳圖片與圖片下載功能
- php+ajax實(shí)現(xiàn)帶進(jìn)度條的上傳圖片功能【附demo源碼下載】
- PHP結(jié)合jquery ajax實(shí)現(xiàn)上傳多張圖片,并限制圖片大小操作示例
相關(guān)文章
php中serialize序列化與json性能測(cè)試的示例分析
本篇文章介紹了,在php中serialize序列化與json性能測(cè)試的示例分析。需要的朋友參考下2013-04-04
114啦源碼(114la)不能生成地方房產(chǎn)和地方報(bào)刊問(wèn)題4級(jí)頁(yè)面0字節(jié)的解決方法
做了個(gè)網(wǎng)址導(dǎo)航站,用的是114la的源碼,沒(méi)想到里面有很多問(wèn)題,官方也沒(méi)出補(bǔ)丁,沒(méi)辦法,自己學(xué)著改吧2012-01-01
PHP中使用strpos函數(shù)實(shí)現(xiàn)屏蔽敏感關(guān)鍵字功能
這篇文章主要介紹了PHP中使用strpos函數(shù)實(shí)現(xiàn)屏蔽敏感關(guān)鍵字功能,本文用在發(fā)布文章保存到數(shù)據(jù)庫(kù)時(shí)前攔截敏感關(guān)鍵,需要的朋友可以參考下2014-08-08
PHP對(duì)稱加密算法(DES/AES)類的實(shí)現(xiàn)代碼
本篇文章主要介紹了PHP對(duì)稱加密算法(DES/AES)類的實(shí)現(xiàn)代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
php基于curl擴(kuò)展制作跨平臺(tái)的restfule 接口
這篇文章主要介紹了php基于curl擴(kuò)展制作跨平臺(tái)的restfule 接口的相關(guān)資料以及詳細(xì)的代碼,有需要的小伙伴可以參考下。2015-05-05
php中static 靜態(tài)變量和普通變量的區(qū)別
靜態(tài)變量與普通變量的區(qū)別在哪里呢,對(duì)于許多初學(xué)php的朋友來(lái)講可能不是點(diǎn)擊的明白了,今天我們來(lái)看看php中static 靜態(tài)變量和普通變量的區(qū)別吧,需要的朋友可以參考下2016-12-12

