文件操作

1.检查文件或目录是否存在

bool file_exists  ( string $filename  )      file_exists:检查文件或目录是否存在

bool is_file  ( string $filename  )

bool is_dir  ( string $filename  )

 

2.读取文件

file_get_contents :将整个文件读入一个字符串

fopen :打开文件或URL

r :只读文件打开,将文件指针指向文件头部

r+ :读写方式打开,将文件指针指向文件头部

w :写入方式打开,将文件指针指向文件头并将文件大小截为零,如果文件不存在则尝试创建之

w+ :读写方式打开, 将文件指针指向文件头并将文件大小截为零,如果文件不存在则尝试创建之

fclose : 关闭一个已经打开的文件指针

fgets :从文件指针中读取一行

 

3.字符编码格式

ASCII 、GB2312、GBK、UTF-8

(1)ASCII 、GB2312、GBK、UTF-8 的区别:

ASCLL编码:这是最早的一种编码,很多其他的编码都会兼容ASCILL编码,ASCILL编码包含常用的英文字母,数字,以及一些特殊的字符,还有部分控制字符,一个字符占一个字节

GB2312编码 :该编码方式常用的中文字符,而且兼容ASCILL编码,一个中文占两个字节

GBK编码 :一种中文编码,其兼容GB2312编码,并且比GB2312包含了更多的汉子,一个中文占两个字节

UTF-8编码 :国际化编码方式,包含了世界各国大部分文字,并且兼容ASCILL编码,大部分中文在UTF-8中占三个字节

mb_detect_encoding :检测字符的编码

iconv  :字符串按要求的字符编码来转换

mb_convert_encoding :转换字符的编码

4.文件上传

form表单 : 

enctype="multipart/form-data"

php后台 :

$_FILES

0 :成功

1 :上传文件超出了PHP配置文件中wpload_max_filesize的值

2 :上传文件大小超出了3HTML表单中MAX_FILE_SIZE的值

3 :只有部分上传

4 :没有选择上传文件

5.例子:注册学生信息

html代码:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">

	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1">

	<title>注册学生信息</title>
	  <!-- 引入 -->
	<link rel="stylesheet" type="text/css" href="./bootstrap-3.3.7-dist/css/bootstrap.min.css">
	<link rel="stylesheet" type="text/css" href="./bootstrapvalidator-master/dist/css/bootstrapValidator.min.css">
	<link rel="stylesheet" type="text/css" href="./bootstrap-datetimepicker-master/css/bootstrap-datetimepicker.min.css">
	<script type="text/javascript" src="./bootstrap-3.3.7-dist/js/jquery-3.3.1.min.js"></script>
	<script type="text/javascript" src="./bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
	<script type="text/javascript" src="./bootstrapvalidator-master/dist/js/bootstrapValidator.min.js"></script>
	<script type="text/javascript" src="./bootstrap-datetimepicker-master/js/bootstrap-datetimepicker.min.js"></script>
	 <script type="text/javascript" src="./bootstrap-datetimepicker-master/js/locales/bootstrap-datetimepicker.zh-CN.js"></script>
</head>
<body>
 <div class="container">
	<form class="form-horizontal" name="form1" action="reg.php" method="post" enctype="multipart/form-data">
	  <div class="form-group">
	    <label for="no" class="col-sm-2 control-label">学号</label>
	  <div class="col-sm-10">
	      <input type="text" class="form-control" id="no" name="no" placeholder="请输入学号">
	  </div>
	  </div>
	  <div class="form-group">
	    <label for="name" class="col-sm-2 control-label">姓名</label>
	  <div class="col-sm-10">
	      <input type="text" class="form-control" id="name" name="name" placeholder="请输入姓名">
	  </div>
	  </div>
	      <div class="form-group">
	    <label for="pwd" class="col-sm-2 control-label">性别</label>
	  <div class="col-sm-10">
	      <input type="radio"  id="sex" name="sex" name="sex" value="男">男&nbsp;&nbsp;&nbsp;&nbsp;
	      <input type="radio" id="sex" name="sex" name="sex" value="女">女
	  </div>
	  </div>
	    <div class="form-group">
	    <label for="pwd" class="col-sm-2 control-label">出生日期</label>
	  <div class="col-sm-10">
	      <input type="text" class="form-control" id="birthday" name="birthday" readonly>
	  </div>
	  </div>
	  <div class="form-group">
	    <label for="phone" class="col-sm-2 control-label">照片</label>
	  <div>
	       <input type="hidden" name="MAX_FILE_SIZE" value="1048576">
		   <input type="file" name="file1" id="file1">
		  <!--  <input type="submit" value="上传">	  -->  
	  </div>
	  </div>
	 
	  <div class="form-group">
	    <div class="col-sm-offset-2 col-sm-10">
	      <button type="submit" class="btn btn-primary btn-lg btn-block">注册</button>
	    </div>
	  </div>
  </form>
</div>
<script type="text/javascript">
 $(function(){
 	$('#birthday').datetimepicker({
 		foemat: 'yyyy-mm-dd',
 		minView: 'month',
 		autoclose: true,
 		language: 'zh-CN'
 	 })
 })
	$(function () {
	       $('form').bootstrapValidator({
	       message: 'This value is not valid',
	             feedbackIcons: {
	                        valid: 'glyphicon glyphicon-ok',
	                        invalid: 'glyphicon glyphicon-remove',
	                        validating: 'glyphicon glyphicon-refresh'
	                       },
	            fields: {
	                no: {
	                    message: '请输入学号',
	                    validators: {
	                        notEmpty: {
	                            message: '学号不能为空'
	                        },
	                        stringLength: {
	                            min: 10,
	                            max: 10,
	                            message: '学号必须是10位'
                           },
	               	    numeric: {
	               	    		message: '必须是数值'
	               	    	}
	                   }
	                },
	                name: {
	                    validators: {
	                        notEmpty: {
	                            message: '姓名不能为空'
	                        },
	                       stringLength: {
	                            min: 0,
	                            max: 10,
	                            message: '姓名不能多于10位'
                           },
                           regexp: {   
				                regexp: '^[\u4e00-\u9fa5]+(·[\u4e00-\u9fa5]+)*$',
				                message: '姓名必须是中文'
                            }
	                    }
	                },
	         
	              
	            }
	        });
	    });
</script>
</body>
</html>

结果为:

 

PHP后台代码 :

<?php
define('DS',DIRECTORY_SEPARATOR);



//获取表单中的数据
$no = htmlspecialchars($_POST['no']);
$name = htmlspecialchars($_POST['name']);
$sex = $_POST['sex'];
$birthday = htmlspecialchars($_POST['birthday']);
$file1 = $_FILES['file1'];

if (empty($birthday)) {
	$birthday = null;
}


$file1 = null;
if ($_FILES['file1']['error']==0) {
	$type = strstr($_FILES['file1']['type'],'/',true);
	if ('image' !=$type) {
		die('文件上传必须是图片');
	}

//临时文件
$tmp = $_FILES['file1']['tmp_name'];
$filename = $_FILES['file1']['name'];
//指定上传文件的路径
$filepath = __DIR__ . DS . "image";

if (!file_exists($filepath)) {
	mkdir($filepath,0777);//创建目录
}

$ext = strrchr($filename, '.');
$filename = $filepath . DS . $no . $ext;
// echo $filename;

 //数据处理
$no = htmlspecialchars(addslashes($no));
$name = htmlspecialchars(addslashes($name));
$file1 = $file1['tmp_name'];



$ret = move_uploaded_file($tmp,$filename);
	if(true === $ret){
		//相对路径
		$file1 = './image/' . $no . $ext;
	}else{
		die('上传失败');
	}

}elseif($_FILES['file1']['error'] ==1) {
	die('上传文件大小超出1M');
}elseif($_FILES['file1']['error'] ==2) {
	die('上传文件大小超出1M');
}elseif($_FILES['file1']['error'] ==3) {
	die('上传文件失败');
}

//连接数据库
$conn = @new mysqli("localhost","root","","myschool");
if ($conn->connect_error) {
	die('连接数据库失败');
}

$conn->set_charset('utf8');

//学号查重
$sql = "select * from student where no=?";
if ($stmt = $conn->prepare($sql)) {
	$stmt->bind_param('i',$no);
	$stmt->execute();
	$stmt->store_result();
	$n=$stmt->num_rows;//获取记录数
	if ($n>0) {
		$conn->close();
		die('学号:{$no},已经存在');
	}
}


//构建sql
$sql = "insert into student(no,name,sex,birthday,file1) values(?,?,?,?,?)";


if ($stmt = $conn->prepare($sql)) {
	$stmt->bind_param('issss',$no,$name,$sex,$birthday,$file1);
	$res = $stmt->execute();
	if ($res===true) {
		echo <<<STR
       <script type="text/javascript">
         window.location.href="list.php"
       </script>
STR;
  exit;
	}else{
		// var_dump($stmt);
		$conn->close();
		die($stmt->error);//出错信息
	}
}

//关闭数据库连接
$conn->close();


显示个人信息的代码 :

<?php

const PAGE_SIZE = 5;//每页多少条记录
$page = 1;//默认第一页
if (isset($_GET['page'])) {
	 $page = $_GET['page'];
}

echo $page;
$conn = @new mysqli('localhost','root','','myschool');
if ($conn->connect_error) {
	die('连接数据库失败');
}
$conn->set_charset('utf8');

//计算记录总数
$sql = "select count(*) from student";
$result = $conn->query($sql);
$data = $result->fetch_row();
$count = $data[0]; //总记录数

//总页数,取整数(ceil,floor,round)
$page_count = ceil($count/PAGE_SIZE);


//page=1,$index=0;page=2,$index=2;page=3,$index=4
$index = ($page-1)* PAGE_SIZE;

$sql = "select no,name,sex,birthday from student limit $index," . PAGE_SIZE;
$result = $conn->query($sql);

$arr = array();
while ($row = $result->fetch_assoc()) {
	$arr[] = $row;
}

$result->free();
$conn->close();

?>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">

	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1">

	<title>Document</title>
	  <!-- 引入 -->
	<link rel="stylesheet" type="text/css" href="./bootstrap-3.3.7-dist/css/bootstrap.min.css">
	<link rel="stylesheet" type="text/css" href="./bootstrapvalidator-master/dist/css/bootstrapValidator.min.css">
	<link rel="stylesheet" type="text/css" href="./bootstrap-datetimepicker-master/css/bootstrap-datetimepicker.min.css">
	<script type="text/javascript" src="./bootstrap-3.3.7-dist/js/jquery-3.3.1.min.js"></script>
	<script type="text/javascript" src="./bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
	<script type="text/javascript" src="./bootstrapvalidator-master/dist/js/bootstrapValidator.min.js"></script>
	<script type="text/javascript" src="./bootstrap-datetimepicker-master/js/bootstrap-datetimepicker.min.js"></script>
	 <script type="text/javascript" src="./bootstrap-datetimepicker-master/js/locales/bootstrap-datetimepicker.zh-CN.js"></script>
</head>
<body>
<div class="container">
 <div class="panel panel-primary">
   <div class="panel-heading">个人信息</div>
	<table class="table table-bordered table-hover">

		<tr>
			<th>学号</th>
			<th>姓名</th>
			<th>性别</th>
			<th>出生日期</th>
		</tr>
		<?php foreach ($arr as $row) { ?>
		<tr>
			<td><?php echo $row['no'] ?></td>
			<td><a href="info.php"><?php echo $row['name'] ?></a></td>
			<td><?php echo $row['sex'] ?></td>
			<td><?php echo $row['birthday'] ?></td>
		</tr>
		<?php } ?>
	</table>
	<!-- 分页 -->
	<nav aria-label="...">
	  <ul class="pager">
	    <?php if ($page-1>0) { ?>
	   	  <li><a href="<?php echo $_SERVER['PHP_SELF'] ?>?page=<?php echo $page-1 ?>">上一页</a></li>
	    <?php } ?>
	   
	   <?php if($page+1<=$page_count){ ?>
	     <li><a href="<?php echo $_SERVER['PHP_SELF'] ?>?page=<?php echo $page+1 ?>">下一页</a></li>
	    <?php } ?>
	  </ul>
</nav>
	</div>
</div>
</body>

结果为:

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值