
批改狀態(tài):合格
老師批語(yǔ):
// 多文件上傳
function upload_muifile($name = 'files') {
if (isset($_FILES[$name]) && $_FILES[$name]['error']) {
foreach ($_FILES[$name]['error'] as $key => $error) {
if ($error) {
echo "<p style='color:red;'>" . upload_ero($error) . "</p>";
} else {
$filename = strstr($_FILES[$name]['name'][$key],'.',true);
$ext = strstr($_FILES[$name]['name'][$key],'.');
$path = 'uploads/' . md5($filename . 'hello') . $ext;
if (move_uploaded_file($_FILES[$name]['tmp_name'][$key],$path)) {
echo "<p style='color:green;'>{$_FILES[$name]['name'][$key]},上傳成功!</p>";
echo "<img src='{$path}' width='150'/>";
}
}
}
}
}
// 上傳錯(cuò)誤編碼
function upload_ero(int $error = 0) : string
{
switch ($error) {
case 1:
$msg = '超過(guò)單文件大小!';
break;
case 2:
$msg = '超過(guò)前端表單限制';
break;
case 3:
$msg = '上傳文件不完整';
break;
case 4:
$msg = '沒(méi)有文件被上傳';
break;
case 6:
$msg = '找不到臨時(shí)目錄';
break;
case 7:
$msg = '寫入失敗,請(qǐng)檢查目標(biāo)目錄權(quán)限';
break;
default:
exit('未定義錯(cuò)誤!');
}
return $msg;
}
// 調(diào)用上傳文件函數(shù)
upload_muifile($name = 'files');
?>
<!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.0">
<title>多文件上傳</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
<fieldset>
<legend>多文件上傳</legend>
<input type="hidden" name="MAX_FILE_SIZE" value="10485760">
<input type="file" multiple name="files[]">
<button>上傳</button>
</fieldset>
</form>
</body>
</html>
—
namespace mvc_user;
use PDO;
// 模型類
class Model
{
// 獲取數(shù)據(jù)
public function get_data()
{
$pdo = new PDO('mysql:dbname=phpedu','root','123456');
$stmt = $pdo->prepare('select * from users limit 10');
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
}
namespace mvc_user;
// 視圖
class View
{
// 數(shù)據(jù)顯示
public function fetch($data)
{
$table = '<table border="1" cellspacing="0">';
$table .= '<caption>用戶信息表</caption>
<tr bgcolor="lightyellow">
<td>UID</td>
<td>姓名</td>
<td>性別</td>
<td>工資</td>
<td>郵箱</td>
<td>生日</td>
</tr>';
foreach ($data as $user) {
$table .= '<tr>';
$table .= '<td>' . $user['sid'] . '</td>';
$table .= '<td>' . $user['name'] . '</td>';
$table .= '<td>' . $user['gender'] . '</td>';
$table .= '<td>' . $user['salary'] . '</td>';
$table .= '<td>' . $user['email'] . '</td>';
$table .= '<td>' . $user['birthday'] . '</td>';
$table .= '</tr>';
}
$table .= '</table>';
return $table;
}
}
namespace mvc_user;
use Closure;
require 'Model.php';
require 'View.php';
// 服務(wù)容器
class Cont1
{
// 對(duì)象容器
protected $instances = [];
// 添加對(duì)象
public function bind($alias,Closure $process)
{
$this->instances[$alias] = $process;
}
// 取出對(duì)象
public function make($alias,$params=[]){
return call_user_func_array($this->instances[$alias],[]);
}
}
// 將依賴的對(duì)象添加到容器中
$cont = new Cont1();
$cont->bind('model',function () {return new Model();});
$cont->bind('view',function () {return new view();});
class Controller4
{
public function index(Cont1 $cont)
{
$data = $cont->make('model')->get_data();
return $cont->make('view')->fetch($data);
}
}
$controller = new Controller4();
echo $controller->index($cont);
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)