快捷搜索:  

PHP文件上传

upload.html

<html>
<head>
<title>上载文件表单</title>
<Meta http-equiv="Content-Type" Content="text/html;charset=gb2312">
</head>
<body>
<form enctype="multipart/form-data" action="upload.php" method="post">
<input type="hidden" name=" MAX_FILE_SIZE" value="10000000">
<center>请选择文件:
<input name="userfile" type="file"> 
<input type="submit" value="上传文件">
</center>
</form>
</body>
</html>


upload.php

<html>
<head>
<title>处理上载文件</title>
<Meta http-equiv="Content-Type" Content="text/html;charset=gb2312">
</head>
<body>
<?php
$upload_file_dir=realpath(".")."\files";
if(!is_dir($upload_file_dir)){
mkdir($upload_file_dir,0666);
}
copy($_FILES['userfile']['tmp_name'],$upload_file_dir."\\".$_FILES['userfile']['name']);
if($_FILES['userfile']['error']==4){
echo"没有文件被上传。";        
}
else if($_FILES['userfile']['error']==1 || $_FILES['userfile']['error']==2 || !$_FILES['userfile']['size']){
         echo"上传文件不能超过10M。";
}
else if($_FILES['userfile']['error']==0){
         echo"文件上传成功。";
}
else{
         echo"文件上传失败。";
}
?>
</body>
</html>

预定义变量$_FILES
--------------------------------------------------------------------------------
* $_FILES['userfile']['name'] 客户端机器文件的原名称。
* $_FILES['userfile']['type'] 文件的 MIME 类型,需要浏览器提供该信息的支持,例如“image/gif”。
* $_FILES['userfile']['size'] 已上传文件的大小,单位为字节。
* $_FILES['userfile']['tmp_name'] 文件被上传后在服务端储存的临时文件名。
* $_FILES['userfile']['error'] 和该文件上传相关的错误代码
1. 值:0; 没有错误发生,文件上传成功。
2. 值:1; 上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值。
3. 值:2; 上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值。
4. 值:3; 文件只有部分被上传。
5. 值:4; 没有文件被上传。
顶(0)
踩(0)

您可能还会对下面的文章感兴趣:

最新评论