首页 / PHP / (进阶篇)PHP的文件上传与下载实例
(进阶篇)PHP的文件上传与下载实例
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了(进阶篇)PHP的文件上传与下载实例,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2615字,纯文字阅读大概需要4分钟。
内容图文
![(进阶篇)PHP的文件上传与下载实例](/upload/InfoBanner/zyjiaocheng/148/e8492ba6aa7448daa6872c4e626e64dd.jpg)
1.先来个请求页面upload.html
<html> <head> <title>Administration - upload new files</title> </head> <body> <h1>Upload new news files</h1> <form enctype="multipart/form-data" action="upload.php" method=post> <input type="hidden" name="MAX_FILE_SIZE" value="1000000"> Upload this file: <input name="userfile" type="file"> <input type="submit" value="Send File"> </form> </body> </html>
2.php处理客户端请求的数据upload.php
<html> <head> <title>Uploading...</title> </head> <body> <h1>Uploading file...</h1> <?php //Check to see if an error code was generated on the upload attempt if ($_FILES['userfile']['error'] > 0) { echo 'Problem: '; switch ($_FILES['userfile']['error']) { case 1: echo 'File exceeded upload_max_filesize'; break; case 2: echo 'File exceeded max_file_size'; break; case 3: echo 'File only partially uploaded'; break; case 4: echo 'No file uploaded'; break; case 6: echo 'Cannot upload file: No temp directory specified.'; break; case 7: echo 'Upload failed: Cannot write to disk.'; break; } exit; } // Does the file have the right MIME type? if ($_FILES['userfile']['type'] != 'text/plain') { echo 'Problem: file is not plain text'; exit; } // put the file where we'd like it $upfile = '/uploads/'.$_FILES['userfile']['name']; if (is_uploaded_file($_FILES['userfile']['tmp_name'])) { if (!move_uploaded_file($_FILES['userfile']['tmp_name'], $upfile)) { echo 'Problem: Could not move file to destination directory'; exit; } } else { echo 'Problem: Possible file upload attack. Filename: '; echo $_FILES['userfile']['name']; exit; } echo 'File uploaded successfully<br><br>'; // reformat the file contents $fp = fopen($upfile, 'r'); $contents = fread ($fp, filesize ($upfile)); fclose ($fp); $contents = strip_tags($contents); $fp = fopen($upfile, 'w'); fwrite($fp, $contents); fclose($fp); // show what was uploaded echo 'Preview of uploaded file contents:<br><hr>'; echo $contents; echo '<br><hr>'; ?> </body> </html>
3.php文件下载
<?php $filePath = "template/";//此处给出你下载的文件在服务器的什么地方 $fileName = "template.xls"; //此处给出你下载的文件名 $file = fopen($filePath . $fileName, "r"); // 打开文件 //输入文件标签 Header("Content-type:application/octet-stream "); Header("Accept-Ranges:bytes "); Header("Accept-Length: " . filesize($filePath . $fileName)); Header("Content-Disposition: attachment; filename= " . $fileName); //输出文件内容 echo fread($file, filesize($filePath . $fileName)); fclose($file); exit; ?>
以上就是(进阶篇)PHP的文件上传与下载实例的内容,更多相关内容请关注PHP中文网(www.gxlcms.com)!
内容总结
以上是互联网集市为您收集整理的(进阶篇)PHP的文件上传与下载实例全部内容,希望文章能够帮你解决(进阶篇)PHP的文件上传与下载实例所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。