</form〉
<?
php
ﻩ$myfile= $_FILES['myfile'][’tmp_name’];
if($my"")
{
if(copy($myfile,$_FILES[’myfile']['name’]))
{
echo”上传成功";
}else
{
echo ”上传失败";
}
unlink($myfile);
} else
{
echo"没有可上传得文件";
}
?
〉
8、利用php中得文件处理技术编写程序如下:
请运行该程序并理解。
〈?
php
$= 'hello/1、txt’;
$somecontent="我先被写入得!
\r\n”;
$somecontent1=”我后被写入得!
\r\n”;
// 首先我们要确定文件存在并且可写
if(is_writable($)) {
// 在这个例子里,将使用只写模式打开$
//文件指针将会在文件得开头,如果原来文件中有其她数据,则会被删除
if (!
$handle=fopen ($,”w”)){
print ”不能打开文件$";
exit;
}//将$somecontent写入到我们打开得文件中.
if (!
fwrite($handle, $somecontent)){
print”不能写入到文件$";
exit;
}
print”成功地将\" $somecontent\”写入到文件$
fclose($handle);
ﻩ$handle = fopen($, ”w");//再次打开
//继续打开文件写入另外一个字符串,原来写入得数据则被清除
fwrite($handle, $somecontent1);
fclose($handle);
ﻩprint"成功地将\"$somecontent1\"写入到文件$";
} else{
print"文件$不可写";
}?
>
9、请编写程序实现动态页面转化为静态页面。
10、使用类编写程序如下:
请运行该程序并理解。
〈?
php
classFoo//定义基类
{
ﻩ//使用static关键字定义变量
publicstatic$my_static='foo';
publicfunctionstaticValue(){
returnself:
:
$my_static;
}
}
class Bar extendsFoo//创建基类
{
public functionfooStatic(){
returnparent:
:
$my_static; //返回基类得变量$my_static
}
}
//打印基类得$my_static变量
//虽然没有创建类实例,但就是可以直接访问static变量
print'Foo:
:
$my_static结果为’、Foo:
:
$my_static、”<br>”;
$foo=newFoo();//创建基类实例
print$foo—>staticValue()、 "〈br>”;//通过方法放回static变量
print$foo-〉my_static、 ”"; //$my_static 为未定义得属性,所以为空
//通过子类访问$my_static
printBar:
:
$my_static、"";
$bar=newBar();
print$bar->fooStatic(); //返回父类得static变量?
〉
11、利用类编写程序实现文件上传.见文件夹3-10。
php
$Submit= $_FILES['Submit’]['tmp_name’];
if($Submit!
='')
{
$['file’] =$file;
$['name']=$;
$[’size'] = $;
$['type’] =$;
$=array(’gif','jpg’,’jpge’,’png',’txt');
$savepath="C:
\AppServ\myphp\";
$maxsize=0;
$overwrite =0;
$upload=newupload($,$,$savepath, $, $overwrite, $maxsize);
if(!
$upload—〉run())
{
echo"上传失败"、$upload->errmsg();
}}?
〉
12、php显示中文时,经常出现乱码,请编写一个编码转换类,从而实现编码得自动转换.
〈?
php
class changecode{
ﻩprivate$str;
private $input;
private$output;
function__construct($input='’,$output='gb2312'){
$this—〉input=$input;
$this—〉output = $output;
}
ﻩprotected functionchgcode($str){
if($this->input!
='’){
ﻩ$this—>str =iconv($input,$output,$str);
ﻩ}else{
ﻩ$this-〉str=mb_convert_encoding($str,$output);
ﻩ}
}
ﻩfunctiongetStr($str){
$this—>chgcode($str);
return$this—〉str;
ﻩ}
functionsetInput($input){
ﻩ$this->input=$input;
ﻩ}
function setOutput($output){
ﻩ$this—>output=$output;
}
functiongetInput(){
ﻩﻩreturn$this—>input;
ﻩ}
function getOutput(){
ﻩreturn $this—〉output;
ﻩ}
}
?
〉
13、做web开发时,需要对各种情况作出处理,并输出相应得处理。
请编写一个输出类,根据不同得情况,输出不同得处理结果。
〈formmethod=”post" action=”111、php ”>
ﻩ
ﻩ〈td>请输入用户名称:
td>
ﻩ<td>〈inputname="name"type="text" size="20” value=”"/></td>
〈/tr〉
<tr〉
〈td〉〈inputtype="submit”value="确认"/>〈/td>
</tr〉
</form>
<?
php
include_once"function、php";
if($_POST[’name’]!
= ”范春喜"){
ﻩﻩ$tm—> refurbish('用户名错误');
ﻩ}else{
$tm->skipachange(’登陆成功’,'main、php’);
}
?
〉
function、php
〈?
php
classTakeMsg{
ﻩfunction __construct(){
ﻩ}
ﻩfunctionsimpleMsg($mess){
ﻩecho$mess;
ﻩ}ﻩ
functionskipachange($mess,$url=NULL){
ﻩif(!
is_null($url)){
ﻩﻩecho "alert(’"、$mess、”');location='”、$url、”’;”;
}else{
echo "
ﻩ}
ﻩ}
ﻩfunctionrefurbish($mess,$sec=3,$url=NULL){
ﻩﻩif(is_null($url)){
ﻩﻩ$hr =$_SERVER[’_REFERER'];
ﻩecho$mess、’〈br〉';
ﻩﻩﻩecho'页面将于'、$sec、’秒后跳转.如果没有跳转,请点击这里〈ahref="'、$hr、’”>返回
ﻩﻩecho'
ﻩ}else{
ﻩecho$mess、'<br〉’;
ﻩecho'页面将于'、$sec、'秒后跳转。
如果没有跳转,请点击这里<ahref=”’、$url、'”〉返回〈/a>';
ﻩﻩecho '
ﻩ}
}
}
$tm= newTakeMsg();
?
>
〈?
php
ﻩinclude_once"function、php";
$tm->simpleMsg('欢迎光临’);
?
〉
5、实验结果分析与总结
次实验我基本掌握了正则表达式得使用方法,也实现了利用正则表达式限定表单输入值,同时也学会了php获取html表单数据得方法,理解了php中得文件处理得使用方法与类得创建方法,在实验过程中对于函数得编写与调用还比较生疏,需要参考书本与实例来完成,而且对于类得创建与使用还需要多学习。
实 验报告说 明
1、要求每一实验项目书写一份实验报告,并按实验课程归类汇总.
2、实验报告要遵照实验指导书与任课教师得具体要求逐项填写。
3、实验报告正文要求字迹清晰、工整,用钢笔、中性笔或圆珠笔书写。
4、实验报告封面不得有空缺项;实验项目序号指实验项目编号,如:
实验一。
5、实验运行环境之具体实验室与运行软件。
6、实验内容与步骤如书写不够,可加页。