您的位置:首页 > 编程语言 > PHP开发

php文件上传基本操作

2013-01-09 11:00 525 查看
<?php
/*
Array
(
[upload_file] => Array
(
[name] => contacts1.png
[type] => image/png
[tmp_name] => C:\Windows\Temp\php201B.tmp
[error] => 0
[size] => 132443
)

)

*/

$file = $_FILES['upload_file'];
/**
*图片上传
*author:peng
**/

//文件类型
$img_type = array('image/jpg','image/jpeg','image/png','image/pjpeg','image/gif','image/bmp','image/x-png');

//文件大小20M
$max_file_size = 2000000;

//文件保存目录
$upload_dir = '../upload_file/';

//判断文件类型
if(!in_array($file['type'],$img_type)){
echo "文件类型不符".$file['type'];
exit;
}

//判断文件大小
if($file['size']>$max_file_size){
echo "文件过大";
exit;
}

//判断文件目录是否存在,如果不存在,则创建一个
if(!file_exists($upload_dir)){
mkdir($upload_dir);
}

//获取文件名:获取路径信息,
$filename =$file['name']; //文件后缀名
$filetype = end(explode(".",$filename));
$newfilename = $upload_dir.date('Ymdhis',time()).rand(1000,9999).".".$filetype;

//判断文件是否通过http post 的方式上传
if(!is_uploaded_file($file['tmp_name'])){
echo "文件为恶意上传";
exit;
}

//复制文件到指定文件夹
if(move_uploaded_file($file['tmp_name'],$newfilename)){
echo "文件上传成功";

}else{
echo "文件上传失败";
}

?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  php 文件上传