php实验5

实验05 PHP常用功能模块
实验目的
(1)掌握PHP中文件读写的方法; (2)掌握PHP中文件的上传下载的基本方法;
(3)掌握PHP图形处理的技术和方法;
实验内容
一、完成教材P324~326实验5.1(文件命名为SY5_1.php)–文件的读写

  1. 将红色线圈起来的三行代码行去掉,改用file_put_contents( )实现(变为一行代码);
  2. 将绿色线圈起来的三行代码行去掉,改用file_get_contents( )实现(变为一行代码);
    二、完成教材P149~150例5.1(文件命名为SY5_2.php)–文件上传
    在此基础上修改程序,实现文件夹创建、不限定文件类型、程序控制上传文件大小、文件名加上传日时和随机号等功能。

三、完成教材P326~327实验5.2(文件命名为SY5_3.php)–图形处理(选做)
四、完成教材P327~328实验5.3(文件命名为SY5_4.php) --日期处理(选做)

实验结果提交
登录教育在线,将各题的.php文件提交。

(1)sy5_1.php

<!doctype html>
	<style type="text/css">
		div{
			font-size: 18px;
			color: #0000FF;
		}
		li{
			font-size:24px;
			color: #FF0000;
		}
	</style>
<form action="" method="post">
	<table align="center">
		<tr><td bgcolor="#CCCCC"><div>当前最流行的web开发语音:</div></td>
		</tr>
		
		<tr><td><input type="radio" name="vote" value="PHP">PHP</td></tr>
		
		<tr><td><input type="radio" name="vote" value="ASP">ASP</td></tr>
		
		<tr><td><input type="radio" name="vote" value="JSP">JAP</td></tr>
		
		<tr><td><input type="submit" name="bt" value="请投票">P</td></tr>
	</table>	
</form>

<p align="center">注意,学号不能重复</p><br/>

<!-- 以上是输入表单-->
<?php 
$votefile="SY5_01vote.txt";
if(!file_exists($votefile)){
	file_put_contents($votefile,"0|0|0|");
}
if(isset($_POST['bt'])){
	if(isset($_POST['vote'])){
		$vote=$_POST['vote'];
		$votestr=file_get_contents($votefile,fliesize($votefile));
		$votearray=explode("|",$votestr);
		echo "<center><h3>投票完毕!</h3></center>";
		if($vote=='PHP'){
			$votearray[0]++;
			echo "<center>PHP的票数为:<li>".$votearray[0]."</li></center><br/>";
		}
		if($vote=='ASP'){
			$votearray[1]++;
			echo "<center>ASP的票数为:<li>".$votearray[1]."</li></center><br/>";
		}
		
		if($vote=='JSP'){
			$votearray[2]++;
			echo "<center>JSP的票数为:<li>".$votearray[2]."</li></center><br/>";
		}
		$sum=$votearray[]+$votearray[1]+$votearray[2];
		echo"<center>总票数为:<li>".$sum."</li></center><br/>";
		$votestr2=implode("|",$votearray);
		$handle=fopen($votefile,"w+");
		fwrite($handle,$votestr2);
		fclose($handle);
		
	}else{
		echo "<script>alert('未投票!');</script>";
	}
	}
?>

在这里插入图片描述

(2)sy5_2.php

<!doctype html>
<html>
<head>
<meta charset="utf-8">

<title>文件上传</title>

</head>
<center><font size=6 face="华文楷体" color=blue>PHP--文件上传--PHP </font></center>
<form enctype="multipart/form-data" action="" method="post">
	<center>
		<input type="file" name="myfile" >
		<input type="submit" name="up" value="上传文件">
		<br/> 
	</center>

</form>
<?php
if($_POST['up']){
	if($_FILES['myfile']['error']>0)
		echo "<center>"."错误:".$_FILES['myfile']['error']."</center>";
	else{
		if($_FILES['myfile']['error']>0&&$_FILES['myfile']['size']>1024*1024*2){
			$dir='./image/';
			if(!is_dir($dir)){
				mkdir($dir);
			}
			$tmp_filename=$_FILES['myfile']['tmp_name'];
			$filename= $_FILES['myfile']['name'];
			$rand=rand(100,999);
			$filename=date('YmdHis').$rand.$filename;
			if(is_uploaded_file($tmp_filename)){
				if(move_uploaded_file($tmp_filename,"$dir$filename")){
					echo "	<center>"." 文件上传成功:"."</center>";
					echo "	<center>"." 文件大小为:".($_FILES['myfile']['size']/1024)."KB"."</center>";
				}else
					echo "<center>"." 文件上传失败!"."</center>";	
			}
				
			}else
			echo "<center>"." 文件大小超过了2mb!"."</center>";	
		}
}
?>
</html>

在这里插入图片描述

(3)sy5_3.php

<?php
header('Content-type:image/gif');
$image_w=100;
$image_h=25;
$number=range(0,9);
$character=range("Z","A");
$result=array_merge($number,$character);
$string="";
$len=count($result);
for($i=0;$i<4;$i++){
	$new_number[$i]=$result[rand(0,$len-1)];
}
$check_image=imagecreatetruecolor($image_w,$image_h);
$white=imagecolorallocate($check_image,255,255,255);
$black=imagecolorallocate($check_image,0,0,0);
imagefill($check_image,0,0,$white);
for($i=0;$i<100;$i++){
	imagesetpixel($check_image,rand(0,$image_w),rand(0,$image_h),$black);
}
for($i=0;$i<count($new_number);$i++){
	$y=mt_rand(1,$image_h/4);
	$x=mt_rand(1,8)+$image_w*$i/4;
	$color=imagecolorallocate($check_image,mt_rand(0,200),mt_rand(0,200).mt_rand(0,200));
	imagestring($check_image,5,$x,$y,$new_number[$i],$color);
	
}
imagepng($check_image);
imagedestroy($check_image);
?>

由于PHP没有安装GD2扩展库,所以显示不出来
在这里插入图片描述

(4)sy5_4.php

<!doctype html>
<html>
<head>
<meta charset="utf-8">

<title>计算年龄</title>

</head>

<body>
<form method="post" action="" >
	请输入您的生日:
	<input type="text" name="year" size="4"><input type="text" name="month" size="4"><input type="text" name="day" size="2"><input type="submit"name ="bt" value="提交">
</form>
</body>
</html>
<?php
date_default_timezone_set('PRC');
if(isset($_POST['bt'])){
	$year=$_POST['year'];
	$month=$_POST['month'];
	$day=$_POST['day'];
	if(@checkdate($month,$day,$year)){
		echo "你的年龄为:".(date('Y',time())-$year);
		$array=getdate(strtotime("$year-$month-$day"));
		echo"<br>出生时是:".$array['weekday'];
		
	}else{
		echo "<script>alert('无效的日期!');</script>";
	}
}

?>

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值