Home  >  Article  >  Backend Development  >  PHP code to implement uploading and downloading files

PHP code to implement uploading and downloading files

WBOY
WBOYOriginal
2016-07-25 08:58:531070browse
  1. 文件上传下载-bbs.it-home.org
  2. enctype="multipart/form-data">


  3. $dir = 'upload/';
  4. if(is_dir($dir)) {
  5. if ($dh = opendir($dir)) {
  6. while (($file = readdir($dh)) !== false) {
  7. if($file!="." && $file!="..") {
  8. echo "
  9. ";
  10. }
  11. }
  12. closedir($dh);
  13. }
  14. }
  15. ?>
  16. ".$file."
复制代码

2,上传文件源代码 upload_file.php

  1. /**

  2. * File upload and download
  3. * edit bbs.it-home.org
  4. */
  5. if ($_FILES["file"]["error"] > 0)
  6. {
  7. echo "Return Code: " . $_FILES["file"]["error"] . "
    ";
  8. }
  9. else
  10. {
  11. echo "Upload: " . $_FILES["file"]["name"] . "
    ";
  12. echo "Type: " . $_FILES["file"]["type"] . "
    ";
  13. echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb
    ";
  14. echo "Temp file: " . $_FILES["file"]["tmp_name"] . "
    ";
  15. if (file_exists("upload/" . $_FILES["file"]["name"]))
  16. {
  17. echo $_FILES["file"]["name"] . " already exists. ";
  18. }
  19. else
  20. {
  21. move_uploaded_file($_FILES["file"]["tmp_name"],
  22. "upload/" . $_FILES["file"]["name"]);
  23. echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
  24. }
  25. }

  26. //下载文件

  27. header('HTTP/1.1 301 Moved Permanently');
  28. header('Location:files.php');
  29. ?>

复制代码


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn