php上传文件代码实例
在php应用中文件上传的代码是指把本地的文件直接上传到服务器,下面我们把一些文章通过php uploadfile来保存到远程器上,实例代码如下:
- <html>
- <head>
- <title>文件上传</title>
- <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
- </head>
- <body bgcolor="#FFFFFF" text="#000000" leftmargin="0" topmargin="0">
- <?
- require './global.php';
- $updir="uploaddir";
- $form?$form=$form:$form="form1";
- if(!$inputname)$inputname=$_GET['inputname'];
- if($uploadfile){
- $copyfilename=date('YmdHis',time());
- if(!is_dir($updir))mkdir($updir,777);
- $filetype=array(".php",".php3");
- $type=substr($uploadfile['name'],strrpos($uploadfile['name'],'.'),strlen($uploadfile['name'])-strrpos($uploadfile['name'],'.'));
- if(in_array($type,$filetype)){echo "上传的文件不符合格式<a href="javascript教程:history.go(-1)"> 点击返回</a>";exit;}
- $dir="$updir/$copyfilename.$type";
- if(function_exists("ImageJpeg")&&eregi("(.jpg)$",$uploadfile_name)&&$w)
- include("gd.php");
- if(move_uploaded_file($uploadfile,$dir)){
- chmod($dir, 0777);
- $ckupload=2;
- }
- if(!$ckupload){
- if(move_uploaded_file($uploadfile['tmp_name'],$dir)){
- chmod($dir, 0777);
- $ckupload=2;
- }
- }
- if(!$ckupload){
- echo "<CENTER>文件上传不成功,请再次上传,如果多次不成功,请联系管理员<a href="javascript:history.go(-1)"> 点击返回</a></CENTER>";
- exit;
- }
- else {
- echo "上传成功1,<A HREF=?>继续上传</A>";
- echo"<script>parent.$form.$inputname.value='$updir/$copyfilename$type'</script>";
- exit;
- }
- }
- ?>
- <form name="form1" method="post" action="" enctype="multipart/form-data">
- <input class=mmcinb type="file" name="uploadfile" onMouseOver ="this.style.backgroundColor='#FFC864'" onMouseOut ="this.style.backgroundColor='#f3f3f3'" >
- <input class=mmcinb type="submit" name="Submit" value="上传文件" onMouseOver ="this.style.backgroundColor='#FFC864'" onMouseOut ="this.style.backgroundColor='#f3f3f3'" >
- </form>
- //开源代码phpfensi.com
- </body>
- </html>