php上传.jpg限制大小Demo

1.新建uploads文件夹

2.index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>限制大小的文件上传</title>
<style type="text/css">
<!--
body,td,th {
	font-size: 12px;
}
-->
</style></head>
<body>
<center>
<h2>请选择要上传的图片(图片格式为.jpg):</h2>
<form method="post" action="index.php" enctype="multipart/form-data">
	<input type="hidden" name="action" value="upload" />
	<input type="file" name="u_file"/>
	<input type="submit" value="上传" />
</form>
<?php
	if($_POST['action'] == 'upload'){			//判断提交按钮是否为空
		$file_path = "./uploads\\";			//定义图片在服务器中的存储位置
		$picture_name=$_FILES['u_file']['name'];	//获取上传图片的名称
		$picture_name=strstr($picture_name , ".");	//通过strstr()函数截取上传图片的后缀
		if($picture_name!= ".jpg"){					//根据后缀判断上传图片的格式是否符合要求
			echo "<script>alert('上传图片格式不正确,请重新上传'); window.location.href='index.php';</script>";
		}else if($_FILES['u_file']['tmp_name']){
			move_uploaded_file($_FILES['u_file']['tmp_name'],$file_path.$_FILES['u_file']['name']);		
			//执行图片上传
			echo '<h2>图片上传成功!</h2>';
		}
		else
			echo "上传图片失败";
		}
?>
</center>
</body>
</html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值