上传配置
- file_uploads=on|off :服务器是否可以接受文件上传
- max_exexution_time=integer:php脚本在注册一个致命错误之前可以执行的最长时间,单位秒
- memeory_limit=integer:设置脚本可以分配到的最大内存,以MB为单位,可以防止失控的脚本独占服务器内存。
- upload_max_filesize=integer:设置上传文件的最大大小,以MB为单位,此指令必需小于post_max_size。
- upload_tmp_dir=string:设置文件在处理之前必需存放在服务器的一个临时位置,直至文件移动到最终目的地为止。
post_max_size=integer:确定通过post方法可以接受的信息的最大大小,以MB为单位。
$_FILES数组
<form enctype="multipart/form-data" action="demo2.php" method="post">
<input type="hidden" name="MAX_FILR_SIZE" value="1000000">
上传文件:<input type="file" name="userfile">
<input type="submit" name="上传">
</form>
- $_FILES[‘userfile’][‘name’]:表示上传的文件名
- $_FILES[‘userfile’][‘type’]:表示上传的类型
- $_FILES[‘userfile’][‘tmp_name’]:表示上传的文件临时存放的位置
- $_FILES[‘userfile’][‘error’]:表示错误类型,0表示没有任何错误,1表示上载文件的大小超出了约定值即upload_max_filesize,2表示超出html表单的max_file_size元素所指定的最大值,3表示文件只被部分上传,4表示没有上传任何文件。
- $_FILES[‘userfile’][‘size’]:表示上传文件的大小
php上传函数
is_uploaded_file()函数判断指定的文件是否是通过 HTTP POST 上传的。通过http post上传后,文件会存放在临时文件夹下。
move_uploaded_file()上传文件。
<?php
print_r($_FILES);
echo "<br />";
if(is_uploaded_file($_FILES['userfile']['tmp_name'])){
// 在这里移动
// 第一个参数写文件的临时地址
// 第二个参数写文件的目的地
if ($_FILES['userfile']['type']!='image/jpeg') {
echo "<script>alert('只能上传图片');history.back();</script>";
exit;
}
if ($_FILES['userfile']['error']>0) {
exit;
}
define('MAX_SIZE', 200000);
if($_FILES['userfile']['size']>MAX_SIZE){
echo "<script>alert('上传文件的大小过大了');history.back();</script>";
exit;
}
move_uploaded_file($_FILES['userfile']['tmp_name'],'uploads/'.$_FILES['userfile']['name']);
echo "<script>alert('上传成功');location.href='demo3.php?url=".$_FILES['userfile']['name']."';</script>";
}else {
echo '找不到临时文件';
}
?>
注意:兼容性问题,对于图片jpg类型,在IE中是image/pjpeg类型,在火狐中是image/jpeg类型。对于图片png类型,在IE中是image/x-png类型,在火狐中是image/png类型。