简单php文件上传实例

本文章收藏了一款简单php文件上传实例,这真的是一款超级简单的文件上传功能代码,利用php $files来获取要上传文件的,类型,名称与临时名称然后用move_uploaded_file把要上传文件保存到服务器指定目录就OK了.

html代码如下:

  1. <input type= "file " userfile " name= "userfile ">
  2. <input type= "submit " name= "upload " value= "上传 ">

处理上传php代码,代码如下:

  1. <?php
  2. function do_upload($upload_dir,$upload_url)
  3. {
  4. $temp_name = $_files[ 'userfile '][ 'tmp_name '];
  5. $file_name = $_files[ 'userfile '][ 'name '];
  6. $file_name = str_replace( "\ ", " ",$file_name);
  7. $file_name = str_replace( " ' ", " ",$file_name);
  8. $file_path = $upload_dir.$file_name;
  9. $thistime=explode( "- ",date( "y-m-d-h-i-s "));
  10. $thistime=mktime();
  11. $filename=$thistime.substr($file_name,strrpos($file_name, ". "));
  12. //文件名检查
  13. if($file_name = ' ')
  14. {
  15. echo "文件名无效。 ";
  16. exit;
  17. }
  18. if(@move_uploaded_file($_files[ 'userfile '][ 'tmp_name '],$upload_dir.$filename))
  19. {
  20. echo "上传成功。 ";
  21. echo " <meta http-equiv= "refresh " content= "1;url=www.aimeige.com.cn.php "> ";
  22. exit;
  23. }else
  24. {
  25. echo "上传失败。 ";
  26. echo " <meta http-equiv= "refresh " content= "1;url=www.phpfensi.com.php "> ";
  27. exit;
  28. }
  29. echo "end ";
  30. }
  31. ?>

调用方法,代码如下:

  1. $upload_dir ='down.phpfensi.com'; $upload_url='www.phpfensi.com';
  2. do_upload($upload_dir,$upload_url) ;