ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
今天先来介绍文件下载的第一种方法:通过超链接实现文件下载。代码如下: 1、先执行文件上传: <!--upload.html--> <form method="post" action="upload.php" enctype="multipart/form-data"> <input type="hidden" name="MAX_FILE_SIZE" value="1000000" /> <input type="file" name="file[]" /> <input type="file" name="file[]" /> <input type="file" name="file[]" /> <input type="submit" name="sub" value="上传" /> </form> 2、文件上传到服务器之后,将上传的文件相关信息存到数据库当中: /*upload.php*/ if($_POST['sub']=='上传'){ echo '<pre>'; print_r($_FILES); function delEmpty($v){ return $v!=''; } $newUpName = array_filter($_FILES['file']['name'],'delEmpty'); $dirName = 'upload/'.date('ymd'); if(!is_dir($dirName)){ mkdir($dirName,0777,1); } foreach($newUpName as $k=>$v){ if(is_uploaded_file($_FILES['file']['tmp_name'][$k])){ $newPath = $dirName.'/'.time().$_FILES['file']['name'][$k]; if(move_uploaded_file($_FILES['file']['tmp_name'][$k],$newPath)){ $pdo = new PDO('mysql:host=localhost;dbname=upload','upload','upload'); $sql = 'insert into file set fname="'.$_FILES['file']['name'][$k].'",fpath="'.$newPath.'",fsize="'.filesize($newPath).'"'; $pdo->query($sql); }else{ echo '<script type="text/javascript"> alert("上传失败"); history.go(-1); </script>'; } }else{ echo '<script type="text/javascript"> alert("不是通过http post方法上传的"); history.go(-1); </script>'; } } } 3、在file_list页面中查找存储到数据库中的文件信息,并显示,点击下载按钮,即可下载对应的文件: /*file_list.php*/ <!doctype html> <html lang="en"> <head> <meta charset="utf-8" /> <title>上传文件</title> </head> <body> <table cellpadding="0" cellspacing="0" border="1"> <thead> <tr> <td>文件名</td> <td>文件路径</td> <td>文件大小</td> </tr> </thead> <tbody> <?php $pdo = new PDO('mysql:host=localhost;dbname=upload','upload','upload'); $sql = 'select * from file'; $result = $pdo->query($sql); $res = $result->fetchAll(PDO::FETCH_ASSOC); foreach($res as $k=>$v){ echo ' <tr> <td>'.$v['fname'].'</td> <td><a href="'.$v['fpath'].'">下载</a></td> <td>'.$v['fsize'].'</td> </tr> '; } ?> </tbody> </table> </body> </html> 这里需要新建一个upload数据库,然后建一张file数据表,截图如下: ![](https://box.kancloud.cn/c26209c6f9fa9c360dd63d838fe49a35_636x348.jpg =600x400)