
批改狀態(tài):合格
老師批語:給你個(gè)思考題, 想一下如何顯示上傳進(jìn)度
method="POST" enctype="multipart/form-data"
,而且表單發(fā)送前的編碼方式必須是二進(jìn)制編碼。
<?php
namespace compotents\conn
{
include 'upFException.php';
class UploadFile
{
private $oriFileName;//原始文件名
private $fileType;//文件類型
private $error;//錯(cuò)誤類型
private $tmpFileName;//臨時(shí)文件名
private $fileSize;//已上傳文件大小
public function __construct($file)
{
$this->oriFileName = $file['name'];
$this->fileType = $file['type'];
$this->error = $file['error'];
$this->tmpFileName = $file['tmp_name'];
$this->fileSize = $file['size'];
}
public function checkSuccess($fileType):bool//檢測文件是否上傳成功
{
$error = $this->error;
$type = strstr($this->fileType,'/',true);
try {
if ($error > UPLOAD_ERR_OK) :
switch ($error) :
case UPLOAD_ERR_INI_SIZE:
throw new upFException('上傳的文件超過了 php.ini 中 upload_max_filesize 選項(xiàng)限制的值', 1);
break;
case UPLOAD_ERR_FORM_SIZE:
throw new upFException('上傳文件的大小超過了 HTML 表單中 MAX_FILE_SIZE 選項(xiàng)指定的值', 2);
break;
case UPLOAD_ERR_PARTIAL:
throw new upFException('文件只有部分被上傳', 3);
break;
case UPLOAD_ERR_NO_FILE:
throw new upFException('沒有文件被上傳', 4);
break;
case UPLOAD_ERR_NO_TMP_DIR:
throw new upFException('找不到臨時(shí)文件夾', 6);
break;
case UPLOAD_ERR_CANT_WRITE:
throw new upFException('文件寫入失敗', 7);
break;
default:
throw new upFException('未知類型錯(cuò)誤', 8);
endswitch;
endif;
if ($type !== strtolower($fileType)):
throw new upFException('文件類型錯(cuò)誤', 9);
endif;
return true;
}
catch (upFException $e)
{
echo $e;
return false;
}
}
public function saveFile($destDir,$destFileName)
{
$tmp = $this->tmpFileName;
$ori = $this->oriFileName;
$dest = $destDir.$destFileName;
if(is_uploaded_file($tmp))://判斷是否是post方式上傳的文件
move_uploaded_file($tmp,$dest);
endif;
}
}
}
?>
case 'regist':
if($_SERVER['REQUEST_METHOD']==='POST'):
$name = trim($_POST['userName']);
$nc = trim($_POST['userNc']);
$pwd = md5(trim($_POST['pwd1']));
$tpwd = trim($_POST['pwd1']);
$rdate = date('Y-m-d');
//使用用戶名生成文件頭像
$fileName = strstr($name,'.',true).'.png';
//生成一個(gè)文件上傳的對(duì)象
$file = new UploadFile($_FILES['userHeadImg']);
//檢測文件是否上傳成功,參數(shù)是文件上傳的類型限定
if($file->checkSuccess('image')):
//如果文件上傳成功,則保存文件到服務(wù)器上
$destDir = $_SERVER['DOCUMENT_ROOT']. '/php11/0511/images/headImg/';
$file->saveFile($destDir,$fileName);
else:
exit('<script>alert("頭像上傳失敗");location.href="register.php";</script>');
endif;
$data = ['name'=>"$name",'nc'=>"$nc",'password'=>"$pwd",'tpassword'=>"$tpwd",'regdate'=>"$rdate",'headimg'=>"$fileName"];
//先判斷一下用戶名是否已經(jīng)注冊(cè)
$where = "`name`='$name'";
$res = $user->select($table,$where);
if(count($res)):
exit('<script>alert("郵箱已經(jīng)注冊(cè)過了,可直接登錄");location.href="login.php";</script>');
else:
$rowCount = $user->insert($table,$data); //返回受影響的記錄條數(shù)
if($rowCount):
exit('<script>alert("注冊(cè)成功");location.href="login.php";</script>');
else:
exit('<script>alert("注冊(cè)失敗");location.href="register.php";</script>');
endif;
endif;
else:
die('請(qǐng)求類型錯(cuò)誤!');
endif;
<?php
namespace compotents\conn
{
include 'upFException.php';
class UploadMulFile
{
private $oriFileName = [];//原始文件名
private $fileType = [];//文件類型
private $error = [];//錯(cuò)誤類型
private $tmpFileName = [];//臨時(shí)文件名
private $fileSize = [];//已上傳文件大小
public function __construct($file)
{
$this->oriFileName = $file['name'];
$this->fileType = $file['type'];
$this->error = $file['error'];
$this->tmpFileName = $file['tmp_name'];
$this->fileSize = $file['size'];
}
public function checkSuccess($fileType):bool//檢測文件是否上傳成功
{
$errors = $this->error;
foreach($errors as $key => $error ):
$type = strstr($this->fileType[$key],'/',true);
$ori = $this->oriFileName[$key];
try {
if ($error > UPLOAD_ERR_OK) :
switch ($error) :
case UPLOAD_ERR_INI_SIZE:
throw new upFException($ori.'上傳的文件超過了 php.ini 中 upload_max_filesize 選項(xiàng)限制的值', 1);
break;
case UPLOAD_ERR_FORM_SIZE:
throw new upFException($ori.'上傳文件的大小超過了 HTML 表單中 MAX_FILE_SIZE 選項(xiàng)指定的值', 2);
break;
case UPLOAD_ERR_PARTIAL:
throw new upFException($ori.'文件只有部分被上傳', 3);
break;
case UPLOAD_ERR_NO_FILE:
throw new upFException($ori.'沒有文件被上傳', 4);
break;
case UPLOAD_ERR_NO_TMP_DIR:
throw new upFException($ori.'找不到臨時(shí)文件夾', 6);
break;
case UPLOAD_ERR_CANT_WRITE:
throw new upFException($ori.'文件寫入失敗', 7);
break;
default:
throw new upFException($ori.'未知類型錯(cuò)誤', 8);
endswitch;
endif;
if ($type !== strtolower($fileType)):
throw new upFException($ori.'文件類型錯(cuò)誤', 9);
endif;
}
catch (upFException $e)
{
echo $e;
return false;
}
endforeach;
return true;//如果沒有錯(cuò)誤,返回true
}
public function saveFile($destDir):array
{
$tmps = $this->tmpFileName;
$images = [];
foreach($tmps as $key => $tmp):
$tmp = $this->tmpFileName[$key];
$ori = $this->oriFileName[$key];
$images[$key] = $ori;
$dest = $destDir.$ori;
if(is_uploaded_file($tmp))://判斷是否是post方式上傳的文件
move_uploaded_file($tmp,$dest);
endif;
endforeach;
return $images;
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/form.css">
<title>Document</title>
</head>
<body>
<div class="showgoods">
<?php
require 'autoLoad.php';
use compotents\conn\UploadMulFile;
if(count($_FILES)):
$file = new UploadMulFile($_FILES['goods']);
if($file->checkSuccess('image')):
//如果文件上傳成功,則保存文件到服務(wù)器上
$destDir = $_SERVER['DOCUMENT_ROOT']. '/php11/0511/images/goods/';
$images = $file->saveFile($destDir,$fileName);
endif;
?>
<div style="display: flex;flex-flow: row wrap;justify-content: space-evenly;
align-items: center;">
<?php foreach($images as $image):?>
<div
style="width: 180px;height:150px; display:flex;justify-content:cetner;align-items: center;margin: 10px 10px;">
<img src="images/goods/<?php echo $image ?>" alt=""></div>
<?php endforeach; ?>
</div>
<?php
else:
?>
<form action="" method="POST" enctype="multipart/form-data">
<div>
<input type="file" name="goods[]" multiple>
</div>
<div><button type="submit">上傳</button></div>
</form>
<?php
endif;
?>
</div>
</body>
</html>
<?
namespace compotents\conn
{
use Exception;
class upFException extends Exception
{
// 在異常子類中,可以訪問并重寫Exception中的四個(gè)屬性,通過__toString()格式化異常輸出信息
public function __toString()
{
return <<< UPLOAD
<style>
table {border-collapse: collapse;border:1px solid black;text-align: center;}
td {border:1px solid black;padding: 5px;}
tr:first-of-type {background-color:#eee;
}
tr:last-of-type td {color: coral;}
</style>
<table>
<tr><td>代碼</td><td>信息</td><td>文件</td><td>行號(hào)</td></tr>
<tr><td>$this->code</td><td>$this->message</td><td>$this->file</td><td>$this->line</td></tr>
</table>
UPLOAD;
}
}
}
?>
微信掃碼
關(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)