当前位置:编程学习 > php >>

php文件上传函数

在php教程中要上传文件那简单的利用move_uploaded_file() 函数将上传的文件移动到新位置。若成功,则返回 true,否则返回 false。
语法
move_uploaded_file(file,newloc)参数 描述
file 必需。规定要移动的文件。
newloc 必需。规定文件的新位置

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.zzzyk.com/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>php文件上传函数</title>
</head>

<body>

<form enctype="multipart/form-data" action="upload.php" method="post">
    <input type="hidden" name="max_file_size" value="30000" />
    <input name="userfile" type="file" />
    <input type="submit" value="send file" />
</form>
</body>
</html>

<?
if( $_post )
{
 if( uploadfile( "userfile" ) )
 {
  echo '文件上传成功';
 }
 else
 {
  echo '文件上传失败';
 }
}
//参数   $file   为   前台file控件的name;
function     uploadfile($file)
{
    $uploaddir   =   $_server[ 'document_root ']. '/www.zzzyk.com/uploadfile/ ';
    $file_name   =   $uploaddir.rand(1,1000000). ". ".fileextend($_files[$file][ 'name ']);
    if   (move_uploaded_file($_files[$file][ 'tmp_name '],$file_name))  
    {
    return   true;
    }
    else
    {
        return   false;
    }
}
?>

提示和注释
注释:本函数仅用于通过 http post 上传的文件。

注意:如果目标文件已经存在,将会被覆盖。

补充:Php教程,Php入门 
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,