<?php
/* 定义常量 */
//定义允许上传的MIME格式
define("UPLOAD_IMAGE_MIME", "image/pjpeg,image/jpg,image/jpeg,image/gif,image/x-png,image/png");
//图片允许大小,字节
define("UPLOAD_IMAGE_SIZE", 102400);
//图片大小用KB为单位来表示
define("UPLOAD_IMAGE_SIZE_KB", 100);
//图片上传的路径
define("UPLOAD_IMAGE_PATH", "./upload/");
//获取允许的图像格式
$mime = explode(",", USER_FACE_MIME);
$is_vaild = 0;
//遍历所有允许格式
foreach ($mime as $type)
{
if ($_FILES['image']['type'] == $type)
{
$is_vaild = 1;
}
}
//如果格式正确,并且没有超过大小就上传上去
if ($is_vaild && $_FILES['image']['size']<=USER_FACE_SIZE && $_FILES['image']['size']>0)
{
if (move_uploaded_file($_FILES['image']['tmp_name'], USER_IMAGE_PATH . $_FILES['image']['name']))
{
$upload_msg ="上传图片成功!";
}
else
{
$upload_msg = "上传图片文件失败";
}
}
else
{
$upload_msg = "上传图片失败,可能是文件超过". USER_FACE_SIZE_KB ."KB、或者图片文件为空、或文件格式不正确";
}
//解析模板文件
$smarty->assign("upload_msg", $upload_msg);
$smarty->display("upload.tpl");
?>
模板文件:upload.tpl
{if $upload_msg != ""}
callbackMessage("{$upload_msg}");
{/if}
//回调的JavaScript函数,用来在父窗口显示信息
function callbackMessage(msg)
{
//把父窗口显示消息的层打开
parent.document.getElementById("message").style.display = "block";
//把本窗口获取的消息写上去
parent.document.getElementById("message").innerHTML = msg;
//并且设置为3秒后自动关闭父窗口的消息显示
setTimeout("parent.document.getElementById('message').style.display = 'none'", 3000);
}