php上传文件代码实例

在php应用中文件上传的代码是指把本地的文件直接上传到服务器,下面我们把一些文章通过php uploadfile来保存到远程器上,实例代码如下:

  1. <html>
  2. <head>
  3. <title>文件上传</title>
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  5. </head>
  6. <body bgcolor="#FFFFFF" text="#000000" leftmargin="0" topmargin="0">
  7. <?
  8. require './global.php';
  9. $updir="uploaddir";
  10. $form?$form=$form:$form="form1";
  11. if(!$inputname)$inputname=$_GET['inputname'];
  12. if($uploadfile){
  13. $copyfilename=date('YmdHis',time());
  14. if(!is_dir($updir))mkdir($updir,777);
  15. $filetype=array(".php",".php3");
  16. $type=substr($uploadfile['name'],strrpos($uploadfile['name'],'.'),strlen($uploadfile['name'])-strrpos($uploadfile['name'],'.'));
  17. if(in_array($type,$filetype)){echo "上传的文件不符合格式<a href="javascript教程:history.go(-1)"> 点击返回</a>";exit;}
  18. $dir="$updir/$copyfilename.$type";
  19. if(function_exists("ImageJpeg")&&eregi("(.jpg)$",$uploadfile_name)&&$w)
  20. include("gd.php");
  21. if(move_uploaded_file($uploadfile,$dir)){
  22. chmod($dir, 0777);
  23. $ckupload=2;
  24. }
  25. if(!$ckupload){
  26. if(move_uploaded_file($uploadfile['tmp_name'],$dir)){
  27. chmod($dir, 0777);
  28. $ckupload=2;
  29. }
  30. }
  31. if(!$ckupload){
  32. echo "<CENTER>文件上传不成功,请再次上传,如果多次不成功,请联系管理员<a href="javascript:history.go(-1)"> 点击返回</a></CENTER>";
  33. exit;
  34. }
  35. else {
  36. echo "上传成功1,<A HREF=?>继续上传</A>";
  37. echo"<script>parent.$form.$inputname.value='$updir/$copyfilename$type'</script>";
  38. exit;
  39. }
  40. }
  41. ?>
  42. <form name="form1" method="post" action="" enctype="multipart/form-data">
  43. <input class=mmcinb type="file" name="uploadfile" onMouseOver ="this.style.backgroundColor='#FFC864'" onMouseOut ="this.style.backgroundColor='#f3f3f3'" >
  44. <input class=mmcinb type="submit" name="Submit" value="上传文件" onMouseOver ="this.style.backgroundColor='#FFC864'" onMouseOut ="this.style.backgroundColor='#f3f3f3'" >
  45. </form>
  46. //开源代码phpfensi.com
  47. </body>
  48. </html>