2020年5月29日

    1. <!DOCTYPE html>
    2. <html>
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>内容</title>
    6. </head
    7. <body>
    8. h1>单文件上传</h1>
    9. <form action="demo.php" method="post" enctype="multipart/form-data">
    10. <input type="file" name="f">
    11. <button>上传</button>
    12. </form>
    13. <h1>多文件上传</h1>
    14. <form action="demo.php" method="post" enctype="multipart/form-data">
    15. <input type="file" name="f[]">
    16. <input type="file" name="f[]">
    17. <input type="file" name="f[]">
    18. <button>上传</button>
    19. </form>
    20. </body>
    21. </html>
    22. <?php
    23. include './Upload.class.php';
    24. $obj=new Upload('f');
    25. var_dump($obj);
    26. class Upload{
    27. protected $fileInfo;
    28. protected $errorMsg;
    29. protected $size =1024*1024*10;
    30. protected $type =[
    31. 'image/png',
    32. 'image/jpg',
    33. 'image/gif'
    34. ];
    35. protected $rootPath='./uploads/';
    36. public function __construct($name){
    37. $this->fileInfo=$_FILES[$name];
    38. }
    39. public function geterrorMsg(){
    40. return $this->errorMsg;
    41. }
    42. public function $size($data=null){
    43. if ($data !== null && is_numeric($data)){
    44. $this->size = $data;
    45. }
    46. return $this->size;
    47. }
    48. public function type($data=null){
    49. if($data !== null && is_array($data)){
    50. $this->type =$data;
    51. }
    52. return $this->type;
    53. }
    54. public function rootPath($data=null){
    55. if ($data !== null && file_exists($data)){
    56. $data = rtrim($data,'/').'/';
    57. $this->rootPath = $data;
    58. }
    59. return $this->rootPath;
    60. }
    61. }
    62. include './Upload.class.php';
    63. $obj=new Upload('f');
    64. $obj->size(1024*1024*10);
    65. if($obj->upload()){
    66. }else{
    67. echo $obj->geterrorMsg();
    68. }
    69. class Upload{
    70. protected $fileInfo;
    71. protected $errorMsg;
    72. protected $size =1024*1024*10;
    73. protected $type =[
    74. 'image/png',
    75. 'image/jpg',
    76. 'image/gif',
    77. 'application/x-zip-dompressed',
    78. 'application/vnd.ms-excel',
    79. ];
    80. protected $rootPath='./uploads/';
    81. public function __construct($name){
    82. $this->fileInfo=$_FILES[$name];
    83. }
    84. public function upload(){
    85. $error = $this->fileInfo['error'];
    86. $size=$this->fileInfo['size'];
    87. $type=$this->fileInfo['type'];
    88. $checkRes=$this->check($error,$size,$type);
    89. if($checkRes == false);
    90. }
    91. }
    92. public function geterrorMsg(){
    93. return $this->errorMsg;
    94. }
    95. public function $size($data=null){
    96. if ($data !== null && is_numeric($data)){
    97. $this->size = $data;
    98. }
    99. return $this->size;
    100. }
    101. public function type($data=null){
    102. if($data !== null && is_array($data)){
    103. $this->type =$data;
    104. }
    105. return $this->type;
    106. }
    107. public function rootPath($data=null){
    108. if ($data !== null && file_exists($data)){
    109. $data = rtrim($data,'/').'/';
    110. $this->rootPath = $data;
    111. }
    112. return $this->rootPath;
    113. }
    114. protected function check($error,$size,$type){
    115. if($this->checkError($error)==false){
    116. return false;
    117. }
    118. if($this->checkSize($size)==false){
    119. return false;
    120. }
    121. return true;
    122. }
    123. protected function checkError($error)
    124. {
    125. if($error >0){
    126. switch($error){
    127. case 1:
    128. $this->errorMsg('上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值');
    129. break;
    130. case 2:
    131. $this->errorMsg('上传的文件大小超过HTML表单中MAX_FILE_SIZE 选项指定的值');
    132. break;
    133. case 3:
    134. $this->errorMsg('文件只有部分上传');
    135. break;
    136. case 4:
    137. $this->errorMsg('没有文件被上传');
    138. break;
    139. case 6:
    140. $this->errorMsg('找不到临时文件夹');
    141. break;
    142. case 7:
    143. $this->errorMsg('文件写入失败');
    144. break;
    145. }
    146. return false;
    147. }
    148. return true;
    149. }
    150. protected function checkSize($size){
    151. if($size>$this->size){
    152. $this->errorMsg="超出大小";
    153. return false;
    154. }
    155. return true;
    156. }
    157. protected function checkType($type){
    158. if(! in_array($type,$this->type)){
    159. $this->errorMsg='不被允许上传的类型:'.$type;
    160. return false;
    161. }
    162. return true;
    163. }
    164. protected function generateName($tmp_name,$name){
    165. $fileData=file_get_contents(tmp_name);
    166. $fileMd5=md5($fileData);
    167. $id=uniqid();
    168. //获得文件后缀
    169. $fileExt=pathinfo($file['name'],PATHINFO_EXTENSION);
    170. //拼接成文件名
    171. $fileName=$fileMd5 .'-'.$id.'.'.$fileExt;
    172. return $fileName;
    173. }
    174. protected function sava($tmp_name,$fileName){
    175. $path=$this->rootPath .$filename;
    176. if(is_uploaded_file($tmp_name)&& move_uploaded_file($tmp_name,$path)){
    177. return $path;
    178. }else{
    179. $this->errorMsg ="上传失败";
    180. return false;
    181. }
    182. }
    183. }
    184. include './Upload.class.php';
    185. $obj=new Upload('f');
    186. $obj->size(1024*1024*10);
    187. $obj->type([
    188. 'image/png',
    189. 'application/x-masdownload',
    190. ]);
    191. if($obj->upload()){
    192. echo "上传成功"
    193. var_dump($obj->getFilePath());
    194. }else{
    195. echo $obj->getErrorMsg();
    196. }
    197. <!DOCTYPE html>
    198. <html>
    199. <head>
    200. <meta charset="UTF-8">
    201. <title>内容</title>
    202. </head
    203. <body>
    204. h1>单文件上传</h1>
    205. <form action="demo.php" method="post" enctype="multipart/form-data">
    206. <input type="file" name="f">、
    207. <button>上传</button>
    208. </form>
    209. <h1>多文件上传</h1>
    210. <form action="demo.php" method="post" enctype="multipart/form-data">
    211. <input type="file" name="f[]">
    212. <input type="file" name="f[]">
    213. <input type="file" name="f[]">
    214. <button>上传</button>
    215. </form>
    216. </body>
    217. </html>
    218. <?php
    219. include './Upload.class.php';
    220. $obj=new Upload('f');
    221. var_dump($obj);
    222. class Upload{
    223. protected $fileInfo;
    224. protected $errorMsg;
    225. protected $size =1024*1024*10;
    226. protected $type =[
    227. 'image/png',
    228. 'image/jpg',
    229. 'image/gif'
    230. ];
    231. protected $rootPath='./uploads/';
    232. public function __construct($name){
    233. $this->fileInfo=$_FILES[$name];
    234. }
    235. public function geterrorMsg(){
    236. return $this->errorMsg;
    237. }
    238. public function $size($data=null){
    239. if ($data !== null && is_numeric($data)){
    240. $this->size = $data;
    241. }
    242. return $this->size;
    243. }
    244. public function type($data=null){
    245. if($data !== null && is_array($data)){
    246. $this->type =$data;
    247. }
    248. return $this->type;
    249. }
    250. public function rootPath($data=null){
    251. if ($data !== null && file_exists($data)){
    252. $data = rtrim($data,'/').'/';
    253. $this->rootPath = $data;
    254. }
    255. return $this->rootPath;
    256. }
    257. }
    258. include './Upload.class.php';
    259. $obj=new Upload('f');
    260. $obj->size(1024*1024*10);
    261. if($obj->upload()){
    262. }else{
    263. echo $obj->geterrorMsg();
    264. }
    265. class Upload{
    266. protected $fileInfo;
    267. protected $errorMsg;
    268. protected $size =1024*1024*10;
    269. protected $type =[
    270. 'image/png',
    271. 'image/jpg',
    272. 'image/gif',
    273. 'application/x-zip-dompressed',
    274. 'application/vnd.ms-excel',
    275. ];
    276. protected $rootPath='./uploads/';
    277. public function __construct($name){
    278. $this->fileInfo=$_FILES[$name];
    279. }
    280. public function upload(){
    281. $error = $this->fileInfo['error'];
    282. $size=$this->fileInfo['size'];
    283. $type=$this->fileInfo['type'];
    284. $checkRes=$this->check($error,$size,$type);
    285. if($checkRes == false);
    286. }
    287. }
    288. public function geterrorMsg(){
    289. return $this->errorMsg;
    290. }
    291. public function $size($data=null){
    292. if ($data !== null && is_numeric($data)){
    293. $this->size = $data;
    294. }
    295. return $this->size;
    296. }
    297. public function type($data=null){
    298. if($data !== null && is_array($data)){
    299. $this->type =$data;
    300. }
    301. return $this->type;
    302. }
    303. public function rootPath($data=null){
    304. if ($data !== null && file_exists($data)){
    305. $data = rtrim($data,'/').'/';
    306. $this->rootPath = $data;
    307. }
    308. return $this->rootPath;
    309. }
    310. protected function check($error,$size,$type){
    311. if($this->checkError($error)==false){
    312. return false;
    313. }
    314. if($this->checkSize($size)==false){
    315. return false;
    316. }
    317. return true;
    318. }
    319. protected function checkError($error)
    320. {
    321. if($error >0){
    322. switch($error){
    323. case 1:
    324. $this->errorMsg('上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值');
    325. break;
    326. case 2:
    327. $this->errorMsg('上传的文件大小超过HTML表单中MAX_FILE_SIZE 选项指定的值');
    328. break;
    329. case 3:
    330. $this->errorMsg('文件只有部分上传');
    331. break;
    332. case 4:
    333. $this->errorMsg('没有文件被上传');
    334. break;
    335. case 6:
    336. $this->errorMsg('找不到临时文件夹');
    337. break;
    338. case 7:
    339. $this->errorMsg('文件写入失败');
    340. break;
    341. }
    342. return false;
    343. }
    344. return true;
    345. }
    346. protected function checkSize($size){
    347. if($size>$this->size){
    348. $this->errorMsg="超出大小";
    349. return false;
    350. }
    351. return true;
    352. }
    353. protected function checkType($type){
    354. if(! in_array($type,$this->type)){
    355. $this->errorMsg='不被允许上传的类型:'.$type;
    356. return false;
    357. }
    358. return true;
    359. }
    360. protected function generateName($tmp_name,$name){
    361. $fileData=file_get_contents(tmp_name);
    362. $fileMd5=md5($fileData);
    363. $id=uniqid();
    364. //获得文件后缀
    365. $fileExt=pathinfo($file['name'],PATHINFO_EXTENSION);
    366. //拼接成文件名
    367. $fileName=$fileMd5 .'-'.$id.'.'.$fileExt;
    368. return $fileName;
    369. }
    370. protected function sava($tmp_name,$fileName){
    371. $path=$this->rootPath .$filename;
    372. if(is_uploaded_file($tmp_name)&& move_uploaded_file($tmp_name,$path)){
    373. return $path;
    374. }else{
    375. $this->errorMsg ="上传失败";
    376. return false;
    377. }
    378. }
    379. }
    380. include './Upload.class.php';
    381. $obj=new Upload('f');
    382. $obj->size(1024*1024*10);
    383. $obj->type([
    384. 'image/png',
    385. 'application/x-masdownload',
    386. ]);
    387. if($obj->upload()){
    388. echo "上传成功"
    389. var_dump($obj->getFilePath());
    390. }else{
    391. echo $obj->getErrorMsg();
    392. }
    393. <!DOCTYPE html>
    394. <html>
    395. <head>
    396. <meta charset="UTF-8">
    397. <title>内容</title>
    398. </head
    399. <body>
    400. h1>单文件上传</h1>
    401. <form action="demo.php" method="post" enctype="multipart/form-data">
    402. <input type="file" name="f">、
    403. <button>上传</button>
    404. </form>
    405. <h1>多文件上传</h1>
    406. <form action="demo.php" method="post" enctype="multipart/form-data">
    407. <input type="file" name="f[]">
    408. <input type="file" name="f[]">
    409. <input type="file" name="f[]">
    410. <button>上传</button>
    411. </form>
    412. </body>
    413. </html>
    414. <?php
    415. include './Upload.class.php';
    416. $obj=new Upload('f');
    417. var_dump($obj);
    418. class Upload{
    419. protected $fileInfo;
    420. protected $errorMsg;
    421. protected $size =1024*1024*10;
    422. protected $type =[
    423. 'image/png',
    424. 'image/jpg',
    425. 'image/gif'
    426. ];
    427. protected $rootPath='./uploads/';
    428. public function __construct($name){
    429. $this->fileInfo=$_FILES[$name];
    430. }
    431. public function geterrorMsg(){
    432. return $this->errorMsg;
    433. }
    434. public function $size($data=null){
    435. if ($data !== null && is_numeric($data)){
    436. $this->size = $data;
    437. }
    438. return $this->size;
    439. }
    440. public function type($data=null){
    441. if($data !== null && is_array($data)){
    442. $this->type =$data;
    443. }
    444. return $this->type;
    445. }
    446. public function rootPath($data=null){
    447. if ($data !== null && file_exists($data)){
    448. $data = rtrim($data,'/').'/';
    449. $this->rootPath = $data;
    450. }
    451. return $this->rootPath;
    452. }
    453. }
    454. include './Upload.class.php';
    455. $obj=new Upload('f');
    456. $obj->size(1024*1024*10);
    457. if($obj->upload()){
    458. }else{
    459. echo $obj->geterrorMsg();
    460. }
    461. class Upload{
    462. protected $fileInfo;
    463. protected $errorMsg;
    464. protected $size =1024*1024*10;
    465. protected $type =[
    466. 'image/png',
    467. 'image/jpg',
    468. 'image/gif',
    469. 'application/x-zip-dompressed',
    470. 'application/vnd.ms-excel',
    471. ];
    472. protected $rootPath='./uploads/';
    473. public function __construct($name){
    474. $this->fileInfo=$_FILES[$name];
    475. }
    476. public function upload(){
    477. $error = $this->fileInfo['error'];
    478. $size=$this->fileInfo['size'];
    479. $type=$this->fileInfo['type'];
    480. $checkRes=$this->check($error,$size,$type);
    481. if($checkRes == false);
    482. }
    483. }
    484. public function geterrorMsg(){
    485. return $this->errorMsg;
    486. }
    487. public function $size($data=null){
    488. if ($data !== null && is_numeric($data)){
    489. $this->size = $data;
    490. }
    491. return $this->size;
    492. }
    493. public function type($data=null){
    494. if($data !== null && is_array($data)){
    495. $this->type =$data;
    496. }
    497. return $this->type;
    498. }
    499. public function rootPath($data=null){
    500. if ($data !== null && file_exists($data)){
    501. $data = rtrim($data,'/').'/';
    502. $this->rootPath = $data;
    503. }
    504. return $this->rootPath;
    505. }
    506. protected function check($error,$size,$type){
    507. if($this->checkError($error)==false){
    508. return false;
    509. }
    510. if($this->checkSize($size)==false){
    511. return false;
    512. }
    513. return true;
    514. }
    515. protected function checkError($error)
    516. {
    517. if($error >0){
    518. switch($error){
    519. case 1:
    520. $this->errorMsg('上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值');
    521. break;
    522. case 2:
    523. $this->errorMsg('上传的文件大小超过HTML表单中MAX_FILE_SIZE 选项指定的值');
    524. break;
    525. case 3:
    526. $this->errorMsg('文件只有部分上传');
    527. break;
    528. case 4:
    529. $this->errorMsg('没有文件被上传');
    530. break;
    531. case 6:
    532. $this->errorMsg('找不到临时文件夹');
    533. break;
    534. case 7:
    535. $this->errorMsg('文件写入失败');
    536. break;
    537. }
    538. return false;
    539. }
    540. return true;
    541. }
    542. protected function checkSize($size){
    543. if($size>$this->size){
    544. $this->errorMsg="超出大小";
    545. return false;
    546. }
    547. return true;
    548. }
    549. protected function checkType($type){
    550. if(! in_array($type,$this->type)){
    551. $this->errorMsg='不被允许上传的类型:'.$type;
    552. return false;
    553. }
    554. return true;
    555. }
    556. protected function generateName($tmp_name,$name){
    557. $fileData=file_get_contents(tmp_name);
    558. $fileMd5=md5($fileData);
    559. $id=uniqid();
    560. //获得文件后缀
    561. $fileExt=pathinfo($file['name'],PATHINFO_EXTENSION);
    562. //拼接成文件名
    563. $fileName=$fileMd5 .'-'.$id.'.'.$fileExt;
    564. return $fileName;
    565. }
    566. protected function sava($tmp_name,$fileName){
    567. $path=$this->rootPath .$filename;
    568. if(is_uploaded_file($tmp_name)&& move_uploaded_file($tmp_name,$path)){
    569. return $path;
    570. }else{
    571. $this->errorMsg ="上传失败";
    572. return false;
    573. }
    574. }
    575. }
    576. include './Upload.class.php';
    577. $obj=new Upload('f');
    578. $obj->size(1024*1024*10);
    579. $obj->type([
    580. 'image/png',
    581. 'application/x-masdownload',
    582. ]);
    583. if($obj->upload()){
    584. echo "上传成功"
    585. var_dump($obj->getFilePath());
    586. }else{
    587. echo $obj->getErrorMsg();
    588. }

    笔记:
    封装一个类:文件上传的功能
    面向过程:按照操作(功能:文件上传)来编写,一步一步按照顺序去实现功能
    面向对象:按照功能(将文件上传功能下的子功能拆分为一个一个的方法),将子功能抽象成为方法,然后把描述抽象成为属性

    对象:文件上传功能
    属性:文件信息 错误信息 文件最大上传字节 文件限定的类型 文件根保存目录
    方法:接受文件信息 判断是否有错误 判断大小是否超出 判断文件类型是否允许上传 保存文件的操作