Home  >  Article  >  php教程  >  php初学篇-php文件上传教程

php初学篇-php文件上传教程

WBOY
WBOYOriginal
2016-06-13 11:25:28994browse

html表单上传代码




  

下面 是php文件上传功能代码

 

function uploadfile($type,$name,$ext,$size,$error,$tmp_name,$targetname,$upload_dir)
{
$max_size = 2000000;
$file_mimes = array('image/pjpeg','image/jpeg','image/jpg','image/gif','image/png');
$file_exts = array('.jpg','.gif','.png','.jpg','.gif','.png');
$file_path = $upload_dir.$targetname;
if(!is_dir($upload_dir))
{
if(!mkdir($upload_dir))
die("文件上传目录不存在并且无法创建文件上传目录");
if(!chmod($upload_dir,0755))
die("文件上传目录的权限无法设定为可读可写");
}
if($size>$max_size)
die("上传的文件大小超过了规定大小");
if($size == 0)
die("请选择上传的文件");
if(!in_array($type,$file_mimes) || !in_array($ext,$file_exts))
die("请上传符合要求的文件类型");
if(!move_uploaded_file($tmp_name, $file_path))
die("复制文件失败,请重新上传");
switch($error)
{
case 0:
return ;
case 1:
die("上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值");
case 2:
die("上传文件的大小超过了 html 表单中 max_file_size 选项指定的值");
case 3:
die("文件只有部分被上传");
case 4:
die("没有文件被上传");
}
}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn