当前位置:首页 > PHP上传文件的代码

PHP上传文件的代码

点击次数:1409  更新日期:2011-01-04
\n

动态网站开发中,常用的PHP上传文件的代码实例。
\n


\n

<html>


\n

<head>


\n

<meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″>


\n

<title>设计家园(www.dwww.cn)文件上传实例</title>


\n

</head>


\n

<body>


\n

<form enctype=”multipart/form-data” action=upfile.php method=post>


\n

<input type=”hidden” name=”MAX_FILE_SIZE” value=”2000000″>


\n

<input type=file name=upfile size=20>


\n

<input type=submit value=’上传文件’>


\n

</form>


\n

</body>


\n

</html>
<?


\n

function getname(exname){


\n

dir = “../uploadfile/”;


\n

i=1;


\n

if(!is_dir(dir)){


\n

mkdir(dir,0777);


\n

}


\n


while(true){


\n

if(!is_file(dir.i.”.”.exname)){


\n

name=i.”.”.exname;


\n

break;


\n

}


\n

i++;


\n

}


\n


return dir.name;


\n

}


\n


exname=strtolower(substr(_FILES[’upfile’][’name’],(strrpos(_FILES[’upfile’][’name’],’.’)+1)));


\n

uploadfile = getname(exname);


\n


if (move_uploaded_file(_FILES[’upfile’][’tmp_name’], uploadfile)) {


\n

echo “<h2><font color=#ff0000>文件上传成功!</font></h2><br><br>”;


\n

}else {


\n

echo “<h2><font color=#ff0000>文件上传失败!</font></h2><br><br>”;


\n

}


\n

echo “下面是文件上传的一些信息:


\n

<br><br>原文件名:”._FILES[’upfile’][’name’] .


\n

“<br><br>类型:” ._FILES[’upfile’][’type’] .


\n

“<br><br>临时文件名:”._FILES[’upfile’][’tmp_name’].


\n

“<br><br>文件大小:”._FILES[’upfile’][’size’] .


\n

“<br><br>错误代码:”._FILES[’upfile’][’error’];
?>

\n