abstract:<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>上傳</title> </head> <body> <form action="upload.php"&nb
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>上傳</title> </head> <body> <form action="upload.php" method="post" enctype="multipart/form-data"> <input type="file" name="MyFile"> <input type="submit" value="上傳"> </form> </body> </html>
<?php //文件的上傳操作 /* function upload_file($fileinfo,$uploadpath='./upload',$allowext=['png','jpg','jpeg','txt','gif','html'],$filesize=1000000){ // 判斷上傳操作的error值,如果為0,表示上傳成功 if ($fileinfo['error']===0){ //判斷上傳的文件夾是否存在,不存在則創(chuàng)建之 if(!is_dir($uploadpath)){ mkdir($uploadpath,0777,true); } //拿到上傳文件的后綴名,并且統(tǒng)一為小寫格式 $ext = strtolower(pathinfo($fileinfo['name'],PATHINFO_EXTENSION)); //判斷上傳文件名是否符合自定義要求的 if(!in_array($ext,$allowext)){ return '上傳文件后綴名不符要求!'; } //判斷上傳的文件大小是否符合要求 if($fileinfo['size']>$filesize){ return '上傳文件超過規(guī)定大??!'; } //判斷文件是否是通過 HTTP POST 上傳的 if(!is_uploaded_file($fileinfo['tmp_name'])){ return '非法上傳!'; } //上傳后生成唯一的文件名(并帶后綴) $uniname = md5(uniqid(microtime(true),true)).".".$ext; //將上傳文件放入指定目錄中,拼接目錄 $dest = $uploadpath."/".$uniname; //判斷上傳是否成功 if(!move_uploaded_file($fileinfo['tmp_name'],$dest)){ return '文件上傳失?。?#39;; } return '文件上傳成功!'; }else{ //如果上傳操作的error值,為其他值,那么返回他們的錯(cuò)誤說明 switch ($fileinfo['error']){ case 1: $res = '上傳的文件超過了 php.ini 中 upload_max_filesize 選項(xiàng)限制的值。'; break; case 2: $res = '上傳文件的大小超過了 HTML 表單中 MAX_FILE_SIZE 選項(xiàng)指定的值。'; break; case 3: $res = '文件只有部分被上傳。'; break; case 4: $res = '沒有文件被上傳。'; break; case 6: $res = '找不到臨時(shí)文件夾。'; break; case 7: $res = '文件寫入失敗。'; break; } return $res; } } */ //var_dump(upload_file($_FILES['MyFile'])); //文件下載操作 function down_file($filename){ //加入header信息 header("Content-type: application/octet-stream"); header("Accept-Ranges: bytes"); header('Accept-Length:'.filesize($filename)); //網(wǎng)上說Accept-Length換成Content-Length,經(jīng)測(cè)試,都可以 header('Content-Disposition:attachment;filename='.basename($filename)); readfile($filename); exit(); } //down_file('123.jpg'); ?>
老師,題目說的封裝成一個(gè)方法,是不是要放在一個(gè)function中?
Correcting teacher:天蓬老師Correction time:2019-07-31 14:11:46
Teacher's summary:如果只有一個(gè)函數(shù)可以放在一個(gè)function 中, 如果由多個(gè)函數(shù)構(gòu)成, 應(yīng)該創(chuàng)建一個(gè)類, 來封裝這些函數(shù)