php
/*上传图片方法*/
$type = isset($_REQUEST['type']) ? $_REQUEST['type'] : ''; //上传图片
if($action=='upload' && $type == 'img'){
if ($_FILES["timg"]["error"] > 0)
{
echo "error";
}
else
{
$type = $_FILES["timg"]["type"];
$type = explode('/',$type);
if($type[0] == 'image')
{
$dirname = 'images/upload/'.date('md');
$imgname = date('YmdHis').'.jpg';
if(!file_exists($dirname)){
mkdir($dirname,0777,true);//创建目录
}
move_uploaded_file($_FILES["timg"]["tmp_name"],
$dirname.'/' . $imgname);
$path = $dirname.$imgname;
echo 'success';
}
else
{
echo 'error';
}
}
}
html
!--jquery.form.js-->
<script language="javascript" type="application/javascript" src="js/jquery.form.js">
</script>
<!--表单提交-->
<script>
$(function ()
{
<!--jquery.form.js上传图片按钮-->
var ajaxFormOption = {
success: function (result) { //提交成功的回调函数
alert(result);
}
};
$("#upload_button").click(function () {
$("#upload_form").ajaxSubmit(ajaxFormOption);
});
</script>
<form action="cishan.php?action=upload&type=img" method="post" id="upload_form" enctype="multipart/form-data" >
<input type="file" id="timg" name="timg"><input type="button" id="upload_button" value="上传">
</form>