• PHP 实现网页文件上传 及 文件展示


    目录

    效果展示

    部分内容及实现代码

    文件上传

    发送数据到当前页面  并使用php处理

     当提交空白表单时 需要用js提前处理 不进行提交

    提交过后判断文件格式是否接受

    展示文件

     获取文件地址

     开始展示文件

    美化或优化

    完整代码



    效果展示

     

    没学过php  其中php内容全靠复制粘贴

    创建./upload  文件夹 用来存储文件

    用到的技术(一点即可)

    html      css       javascript       php



    部分内容及实现代码



    文件上传

    参考:PHP 文件上传 | 菜鸟教程 (runoob.com)

    文件上传参考菜鸟教程    其中也有改变的地方

    发送数据到当前页面  并使用php处理

    1. <form action="form.php" method="post" onsubmit="return checkinput();" enctype="multipart/form-data">
    2. <input type="file" name="file" id="file">
    3. <input type="submit" class="submit" onclick="validate()" name="submit" value="上 传">
    4. form>

     当提交空白表单时 需要用js提前处理 不进行提交




    提交过后判断文件格式是否接受

    1. if($_SERVER["REQUEST_METHOD"] == "POST" && empty($_POST["file"]) !== false){
    2. $allowedExts = array("gif", "jpeg", "jpg", "png","zip","rar","tar",'tgz',"txt","xml","html","css","js");
    3. $temp = explode(".", $_FILES["file"]["name"]);
    4. // echo $_FILES["file"]["size"];
    5. $extension = end($temp); // 获取文件后缀名
    6. if ((($_FILES["file"]["type"] == "image/gif")
    7. || ($_FILES["file"]["type"] == "image/jpeg")
    8. || ($_FILES["file"]["type"] == "image/jpg")
    9. || ($_FILES["file"]["type"] == "image/pjpeg")
    10. || ($_FILES["file"]["type"] == "image/x-png")
    11. || ($_FILES["file"]["type"] == "image/png")
    12. || ($_FILES["file"]["type"] == "application/octet-stream")
    13. || ($_FILES["file"]["type"] == "application/x-tar")
    14. || ($_FILES["file"]["type"] == "application/x-compressed")
    15. || ($_FILES["file"]["type"] == "application/x-zip-compressed")
    16. || ($_FILES["file"]["type"] == "text/plain")
    17. || ($_FILES["file"]["type"] == "text/xml")
    18. || ($_FILES["file"]["type"] == "text/html")
    19. || ($_FILES["file"]["type"] == "text/css")
    20. || ($_FILES["file"]["type"] == "text/javascript")
    21. )
    22. && ($_FILES["file"]["size"] < 20480000)
    23. && in_array($extension, $allowedExts)){
    24. if ($_FILES["file"]["error"] > 0){
    25. echo "错误:: " . $_FILES["file"]["error"] . "
      "
      ;
    26. }
    27. else{
    28. echo '
    29. ';
    30. move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
    31. }
    32. }
    33. else{
    34. echo '
    35. ';
    36. }
    37. }


    常见文件类型

    1. 常用的MIME类型
    2. .doc application/msword
    3. .docx application/vnd.openxmlformats-officedocument.wordprocessingml.document
    4. .rtf application/rtf
    5. .xls application/vnd.ms-excel application/x-excel
    6. .xlsx application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
    7. .ppt application/vnd.ms-powerpoint
    8. .pptx application/vnd.openxmlformats-officedocument.presentationml.presentation
    9. .pps application/vnd.ms-powerpoint
    10. .ppsx application/vnd.openxmlformats-officedocument.presentationml.slideshow
    11. .pdf application/pdf
    12. .swf application/x-shockwave-flash
    13. .dll application/x-msdownload
    14. .exe application/octet-stream
    15. .msi application/octet-stream
    16. .chm application/octet-stream
    17. .cab application/octet-stream
    18. .ocx application/octet-stream
    19. .rar application/octet-stream
    20. .tar application/x-tar
    21. .tgz application/x-compressed
    22. .zip application/x-zip-compressed
    23. .z application/x-compress
    24. .wav audio/wav
    25. .wma audio/x-ms-wma
    26. .wmv video/x-ms-wmv
    27. .mp3 .mp2 .mpe .mpeg .mpg audio/mpeg
    28. .rm application/vnd.rn-realmedia
    29. .mid .midi .rmi audio/mid
    30. .bmp image/bmp
    31. .gif image/gif
    32. .png image/png
    33. .tif .tiff image/tiff
    34. .jpe .jpeg .jpg image/jpeg
    35. .txt text/plain
    36. .xml text/xml
    37. .html text/html
    38. .css text/css
    39. .js text/javascript
    40. .mht .mhtml message/rfc822


    展示文件

    想着图片之类的还展示图片  其他类型的使用文件名字 + 链接跳转过去 下载或其他

    先用php获取文件地址   全部放到数组里   然后对数组遍历输出即可。

     获取文件地址

    1. $prin = traverseDir('./upload');
    2. $num = count($prin);
    3. /**
    4. * 遍历指定路径的文件夹中的文件
    5. * @param $dirPath 文件绝对路径
    6. * @param $type 遍历方法 默认参数为 $type='all' 返回所有文件作为一维数组返回,如果$type='file',则与多维数组返回
    7. * @return array 检索到文件成功返回内部文件路径数组,失败返回false;
    8. */
    9. function traverseDir($dirPath=false,$type='all'){
    10. //检测是否为文件夹
    11. if(!$dirPath||!is_dir($dirPath)){
    12. return false;
    13. }
    14. $files = array();
    15. //增加一个@抑制错误
    16. if(@$handle = opendir($dirPath)){
    17. while(($file=readdir($handle))!==false){
    18. //排除'.'当前目录和'..'上级目录
    19. if($file != '..' && $file != '.'){
    20. //只记录文件
    21. if($type == 'file'){
    22. if(is_dir($dirPath.DIRECTORY_SEPARATOR.$file)){
    23. //如果是文件夹,则重新遍历该文件的文件
    24. $files[$file] = traverseDir($dirPath.DIRECTORY_SEPARATOR.$file,'file');
    25. //把文件存入数组中
    26. foreach($files[$file] as $k => $v){
    27. if(is_file($v)){
    28. $files[] = $v;
    29. //删除源数组中的对应文件路径
    30. unset($files[$file][$k]);
    31. }
    32. }
    33. //删除源数组中的对应文件路径数组
    34. unset($files[$file]);
    35. }else{
    36. //如果是文件则直接存入数组
    37. $files[] = $dirPath.DIRECTORY_SEPARATOR.$file;
    38. }
    39. }else{//记录含文件
    40. if(is_dir($dirPath.DIRECTORY_SEPARATOR.$file)){
    41. //如果是文件夹,则重新遍历该文件的文件
    42. $files[$file] = traverseDir($dirPath.DIRECTORY_SEPARATOR.$file);
    43. }else{
    44. //如果是文件则直接存入数组
    45. $files[] = $dirPath.DIRECTORY_SEPARATOR.$file;
    46. }
    47. }
    48. }
    49. }
    50. closedir($handle);
    51. }
    52. return $files;
    53. }

     开始展示文件

    1. for($i=0;$i<$num;++$i){
    2. if($i%5==0){
    3. echo '
    ';
  • }
  • $name = substr($prin[$i],9);
  • if(strpos($name,'.png')!== false || strpos($name,'.jpg')!== false){
  • echo '
  • $prin[$i].' ">
  • ';
  • }
  • else{
  • echo '
  • $prin[$i].'">'.$name.'
  • ';
  • }
  • }

  • 美化或优化


    1.  由于php发送表单时,会跳转到别的页面    两种方法解决

      一 使用ajax传输数据   可惜我不会

      二 跳转过去后   再使用js跳转过来 

    2. input的美化   

      可以使input标签隐藏  然后用另外的标签来代替input

      利用javascrip实现



    完整代码

    1. // print_r(traverseDir('./upload'));
    2. $prin = traverseDir('./upload');
    3. $num = count($prin);
    4. /**
    5. * 遍历指定路径的文件夹中的文件
    6. * @param $dirPath 文件绝对路径
    7. * @param $type 遍历方法 默认参数为 $type='all' 返回所有文件作为一维数组返回,如果$type='file',则与多维数组返回
    8. * @return array 检索到文件成功返回内部文件路径数组,失败返回false;
    9. */
    10. function traverseDir($dirPath=false,$type='all'){
    11. //检测是否为文件夹
    12. if(!$dirPath||!is_dir($dirPath)){
    13. return false;
    14. }
    15. $files = array();
    16. //增加一个@抑制错误
    17. if(@$handle = opendir($dirPath)){
    18. while(($file=readdir($handle))!==false){
    19. //排除'.'当前目录和'..'上级目录
    20. if($file != '..' && $file != '.'){
    21. //只记录文件
    22. if($type == 'file'){
    23. if(is_dir($dirPath.DIRECTORY_SEPARATOR.$file)){
    24. //如果是文件夹,则重新遍历该文件的文件
    25. $files[$file] = traverseDir($dirPath.DIRECTORY_SEPARATOR.$file,'file');
    26. //把文件存入数组中
    27. foreach($files[$file] as $k => $v){
    28. if(is_file($v)){
    29. $files[] = $v;
    30. //删除源数组中的对应文件路径
    31. unset($files[$file][$k]);
    32. }
    33. }
    34. //删除源数组中的对应文件路径数组
    35. unset($files[$file]);
    36. }else{
    37. //如果是文件则直接存入数组
    38. $files[] = $dirPath.DIRECTORY_SEPARATOR.$file;
    39. }
    40. }else{//记录含文件
    41. if(is_dir($dirPath.DIRECTORY_SEPARATOR.$file)){
    42. //如果是文件夹,则重新遍历该文件的文件
    43. $files[$file] = traverseDir($dirPath.DIRECTORY_SEPARATOR.$file);
    44. }else{
    45. //如果是文件则直接存入数组
    46. $files[] = $dirPath.DIRECTORY_SEPARATOR.$file;
    47. }
    48. }
    49. }
    50. }
    51. closedir($handle);
    52. }
    53. return $files;
    54. }
    55. echo '
    56. beink






    57. ';
    58. for($i=0;$i<$num;++$i){
    59. if($i%5==0){
    60. echo '
        ';
    61. }
    62. $name = substr($prin[$i],9);
    63. if(strpos($name,'.png')!== false || strpos($name,'.jpg')!== false){
    64. echo '
    65. $prin[$i].' ">
    66. ';
    67. }
    68. else{
    69. echo '
    70. $prin[$i].'">'.$name.'
    71. ';
    72. }
    73. }
    74. echo '
    75. ';
    76. if($_SERVER["REQUEST_METHOD"] == "POST" && empty($_POST["file"]) !== false){
    77. $allowedExts = array("gif", "jpeg", "jpg", "png","zip","rar","tar",'tgz',"txt","xml","html","css","js");
    78. $temp = explode(".", $_FILES["file"]["name"]);
    79. // echo $_FILES["file"]["size"];
    80. $extension = end($temp); // 获取文件后缀名
    81. if ((($_FILES["file"]["type"] == "image/gif")
    82. || ($_FILES["file"]["type"] == "image/jpeg")
    83. || ($_FILES["file"]["type"] == "image/jpg")
    84. || ($_FILES["file"]["type"] == "image/pjpeg")
    85. || ($_FILES["file"]["type"] == "image/x-png")
    86. || ($_FILES["file"]["type"] == "image/png")
    87. || ($_FILES["file"]["type"] == "application/octet-stream")
    88. || ($_FILES["file"]["type"] == "application/x-tar")
    89. || ($_FILES["file"]["type"] == "application/x-compressed")
    90. || ($_FILES["file"]["type"] == "application/x-zip-compressed")
    91. || ($_FILES["file"]["type"] == "text/plain")
    92. || ($_FILES["file"]["type"] == "text/xml")
    93. || ($_FILES["file"]["type"] == "text/html")
    94. || ($_FILES["file"]["type"] == "text/css")
    95. || ($_FILES["file"]["type"] == "text/javascript")
    96. )
    97. && ($_FILES["file"]["size"] < 20480000)
    98. && in_array($extension, $allowedExts)){
    99. if ($_FILES["file"]["error"] > 0){
    100. echo "错误:: " . $_FILES["file"]["error"] . "
      "
      ;
    101. }
    102. else{
    103. echo '
    104. ';
    105. move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
    106. }
    107. }
    108. else{
    109. echo '
    110. ';
    111. }
    112. }
    113. ?>

  • 相关阅读:
    如何避免游戏设计缺陷优漫教育
    .NET周报【10月第2期 2022-10-17】
    好用的js快速排序的方法
    【触想智能】工控一体机与5G物联网技术结合是未来发展趋势
    POL890 LVDS&RGB TO MIPI&LVDS
    神经网络与深度学习-7- 支持向量机2 -PyTorch
    linux下 Mysql8.0 离线安装
    Apache 网站服务基础
    一些 Conda 的常用命令
    云服务器登录方式
  • 原文地址:https://blog.csdn.net/qq_53950686/article/details/126928349