abstrait:function upload_dow_file($filename,$fileInfo, $uploadPath = './upload', $allowExt = ['png', 'jpg', 'jpeg', 'gif
function upload_dow_file($filename,$fileInfo, $uploadPath = './upload', $allowExt = ['png', 'jpg', 'jpeg', 'gif', 'txt', 'html'], $maxSize = 1000000) { // 告訴瀏覽器返回文件的大小 header('Accept-Length:' . filesize($filename)); // 告訴瀏覽器文件做為附件處理,并告訴瀏覽器下載完的文件名 header('Content-Disposition:attachment;filename=' . basename($filename)); // 輸出文件 readfile($filename); // 判斷上傳錯誤號 if ($fileInfo['error'] === 0) { // 獲取文件后綴 $ext = strtolower(pathinfo($fileInfo['name'], PATHINFO_EXTENSION)); // 判斷文件類型 if (!in_array($ext, $allowExt)) { return '非法文件類型!'; } // 判斷文件大小 if ($fileInfo['size'] > $maxSize) { return '超出文件上傳最大值!'; } // 判斷文件上傳方式 if (!is_uploaded_file($fileInfo['tmp_name'])) { return '非法上傳!'; } // 判斷需要移動到的目錄是否存在 if (!is_dir($uploadPath)) { mkdir($uploadPath, 0777, true); } // 生成唯一的文件名 uniqid 生成唯一id microtime 返回當前unix時間戳中的微秒 $uniName = md5(uniqid(microtime(true), true)) . "." . $ext; // 拼接路徑以及文件名 $dest = $uploadPath . "/" . $uniName; // 將文件移動到指定目錄 if (!move_uploaded_file($fileInfo['tmp_name'], $dest)) { return '文件上傳失??!'; } return '文件上傳成功!'; } else { switch ($fileInfo['error']) { case 1: $res = '上傳的文件超過了 php.ini 中 upload_max_filesize 選項限制的值!'; break; case 2: $res = '上傳文件的大小超過了 HTML 表單中 MAX_FILE_SIZE 選項指定的值!'; break; case 3: $res = '文件只有部分被上傳!'; break; case 4: $res = '沒有文件被上傳!'; break; case 6: case 7: $res = '系統(tǒng)錯誤!'; break; } return $res; } } echo upload_dow_file('asd.txt');
在html文件中,代碼為:
<!DOCTYPE html> <html lang="en"> <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>
在upload.php文件中,代碼為:
<?php echo '<pre>'; include 'function.php'; $fileinfo = $_FILES['MyFile']; var_dump(upload_file($fileinfo));
最終下載的呈現(xiàn)的效果如下
每張圖片的編碼都不一樣(上傳的同一張圖片).
在網(wǎng)頁中也有此效果:
Professeur correcteur:查無此人Temps de correction:2019-05-07 09:53:36
Résumé du professeur:完成的不錯。寫完上傳方法后,是不是發(fā)現(xiàn)php很容易。繼續(xù)加油。