作业内容:1. 实例演示单文件上传与多文件上传
<!DOCTYPE html>
<html lang="en">
<?php
// $_FILES: PHP超全局变量数量, 保存着上传文件的全部信息
//printf('<pre>%s</pre>',print_r($_FILES,true));
if(isset($_FILES['mypic'])){
$filename = $_FILES['mypic']['name'];
$tmpName = $_FILES['mypic']['tmp_name'];
$error = $_FILES['mypic']['error'];
if($error > 0){
//echo "文件上传失败<br>";
$tips = '<span style="color:red">上传失败:</span>';
switch($error){
case 1:
$tips.= '上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值。 ';
break;
case 2:
$tips.= '上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值。';
break;
case 3:
$tips.= '文件只有部分被上传。';
break;
case 4:
$tips.= '没有文件被上传。 ';
break;
case 6:
$tips.= '找不到临时文件夹。 ';
break;
case 7:
$tips.= '文件写入失败。 ';
break;
}
echo $tips;
}else{
if(is_uploaded_file($tmpName)){
//添加白名单数组
$whitelist = ['jpg','jpeg','png','gif','bmp'];
//Returns information about a file path
$filetype = pathinfo($filename)['extension'];
//Checks if a value exists in an array
if(in_array($filetype,$whitelist)){
$path = 'myfile/';
$target = $path .md5($filename) . ".{$filetype}";
if(move_uploaded_file($tmpName,$target)){
echo "<p style='color:green'>文件上传成功</p><br>";
echo "<img src='$target' width='150px'>";
}else{
echo "<p style='color:red'>文件移动失败</p><br>";
}
}else{
echo "<p style='color:red'>文件格式错误</p><br>";
}
}else{
echo "<p style='color:red'>非法文件</p><br>";
}
}
}
/**
* 1. $_FILES: 二维数组,每个元素对应一个上传的文件
* 2. name: 原始文件名
* 3. type: 文件类型, mime类型
* 4. tmp_name: 临时目录
* 5. error: 错误代码
* 5. size: 文件大小(字节表示 byte)
*/
?>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<fieldset>
<legend>单文件上传</legend>
<input type="hidden" name="MAX_FILE_SIZE" value="300000">
<input type="file" name="mypic">
<button>上传文件</button>
</fieldset>
</form>
</body>
</html>
if(isset($_FILES['mypic'])){
foreach($_FILES['mypic']['error'] as $key=>$error){
if($error == 0){
$tmpName = $_FILES['mypic']['tmp_name'][$key];
$filename = $_FILES['mypic']['name'][$key];
if(is_uploaded_file($tmpName)){
//添加白名单数组
$whitelist = ['jpg','jpeg','png','gif','bmp'];
//Returns information about a file path
$filetype = pathinfo($filename)['extension'];
//Checks if a value exists in an array
if(in_array($filetype,$whitelist)){
$path = 'myfile/';
$target = $path .md5($filename) . ".{$filetype}";
if(move_uploaded_file($tmpName,$target)){
echo "<p style='color:green'>文件上传成功</p><br>";
echo "<img src='$target' width='150px'>";
}else{
echo "<p style='color:red'>文件移动失败</p><br>";
}
}else{
echo "<p style='color:red'>文件格式错误</p><br>";
}
}else{
echo "<p style='color:red'>非法文件</p><br>";
}
}
<input type="file" name="mypic[]" multiple>
- 实例演示简单分页操作,并理解分页所需参数的意思
<body>
<table>
<caption>员工表</caption>
<thead>
<th>ID</th>
<th>部门</th>
<th>姓名</th>
<th>级别</th>
</thead>
<tbody>
<?php
foreach($res as $user){
extract($user);
echo <<<data
<tr>
<td>$id</td>
<td>$dept</td>
<td>$name</td>
<td>$grade</td>
</tr>
data;
}
?>
</tbody>
</table>
<nav>
<?php
$dir = $_SERVER['PHP_SELF'];
for($i=1;$i<=$pagenum;$i++){
echo <<<nav
<a href="$dir?id=$i">$i</a>
nav;
}
?>
</nav>
</body>
数据获取部分
<?php
//1、连接数据库
//require __DIR__.'/hw.php';
//use PDO;
$dbConfig =[
'type'=>'mysql',
'host' => 'localhost',
'dbname' => 'testsql',
'port'=>'3306',
'charset'=>'utf8',
'username' => 'zolo',
'password' => '123456'
];
//import variables into the current symbol table from an array
extract($dbConfig);
$db = new PDO("$type:host=$host;port=$port;dbname=$dbname;charset=$charset",$username,$password);
// $page = isset($_GET['id'])?$_GET['id']:1;
$page = $_GET['id']??1;
//查询操作
//单条查询
$sqlsumnum = "select count(*) as `sumnum` from `users`";
//$stmt = $db->prepare($sqlquery);
$stmt = $db->prepare($sqlsumnum);
$stmt ->execute();
//多条查询
$stmt ->bindColumn('sumnum',$sumnum);
$stmt->fetch(PDO::FETCH_ASSOC);
$num = 5;
$pagenum = floor($sumnum/$num);
$index = ($page-1)*$num;
//echo "当前页面偏移量:$index<br>";
//echo "$sumnum<br>$pagenum<br>";
$sqlquery = "select * from `users` limit $index,$num";
$stmt = $db->prepare($sqlquery);
$stmt ->execute();
$res = $stmt->fetchAll(PDO::FETCH_ASSOC);
//echo "批量查询成功:<br>";
foreach($res as $user){
//printf('<pre>%s</pre>',print_r($user,true));
extract($user);}
?>