index.php
<!DOCTYPE html>
<head>
<title>Limit size file upload</title>
</head>
<body>
<h2>Please choose the img which is ready to upload(the format of img is jpg)</h2>
<form method="post" action="" enctype="multipart/form-data">
<input type="hidden" name="action" value="upload" />
<input type="file" name="u_file"/>
<input type="submit" value="upload"/>
</form>
<?php
if(isset($_POST['action'])){
if($_POST['action'] == 'upload'){ //Judge the btn if it is null
$file_path = "./uploads"; //define the router of img in server
$img_original_name=$_FILES['u_file']['name']; //get the name of img which is uploaded
$img_postfix=strstr($img_original_name , "."); // cut the postfix by using strstr()
if($img_postfix != ".jpg" && $img_postfix != ".png"){ //Judge the format of img if it is right by postfix
echo "<script>alert('the format is not correct,please retry'); window.location.href='';</script>";
}else if($_FILES['u_file']['tmp_name']){
//Execute upload
if(!is_dir($file_path)){
mkdir($file_path);
}
move_uploaded_file($_FILES['u_file']['tmp_name'],$file_path.'\\'.$_FILES['u_file']['name']); // Linux下的斜杠与此相反
echo '<h2>Upload OK</h2>';
}
else
echo "Upload fail";
}
}
?>
</body>
Ajax+PHP实现异步图片上传