首页 > 网页设计 > ajaxSubmit 提交表单实现图片上传

ajaxSubmit 提交表单实现图片上传

<html>
<head>
<meta content="text/html"; charset=utf8 />
<title>AjaxSubmit提交方法,实现图片上传功能</title>
<script type="text/javascript" language="javascript" src="jquery-1.7.1.min.js"></script>
<script type="text/javascript" language="javascript" src="jquery.form.js"></script>
<script type="text/javascript">
<!--
$(function (){
var options = {
target: '#showmsg',
beforeSubmit:showStart,
success:showSuccess
//dataType:  'html'
};
$('#form').submit(function(){
$(this).ajaxSubmit(options);
return false;
});
});
function showStart(){
//alert('开始提交');
return true;
}
function showSuccess(data){
//alert('<img src="'+data+'"/>');exit;
$("#img").html('<img src="'+data+'"/>');
}
</script>
<form action="chuli.php" method="post" id="form" enctype="multipart/form-data">
<div id="showmsg"></div>
<p id="img">text</p>
<input type="file" name="file" />
<input type="submit" value="提交"/>
</form>
</body>

</html>

chuli.php

<?PHP
//用户只能上传 .gif 或 .jpeg 文件,文件大小必须小于 20 kb:
header("Content:text/html; charset=gbk");
if (($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg") || ($_FILES["file"]["type"] == "image/png")&& ($_FILES["file"]["size"] < 20000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
// echo "上传文件: " . $_FILES["file"]["name"] . "<br />";
//  echo "文件类型: " . $_FILES["file"]["type"] . "<br />";
// echo "文件大小: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
// echo "临时文件: " . $_FILES["file"]["tmp_name"] . "<br />";

if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo "文件: " .$_FILES["file"]["name"] . " 已经存在! ";
}
else
{ $imgname=iconv("UTF-8","GB2312",$_FILES["file"]["name"]);   //变量的编码格式转换
move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" .$imgname);
echo "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "上传失败";
}
file_put_contents('rs.txt',var_export($_FILES,true));
exit();
?>


本文固定链接: http://www.devba.com/index.php/archives/3572.html | 开发吧

报歉!评论已关闭.