PHP文件上传
PHP中文件上传代码
1.fileup.php 上传文件表单
代码
<html>
<head>
<title>文件上传</title>
</head>
<body>
<form enctype="multipart/form-data" action="up.php" method="POST">
<!-- MAX_FILE_SIZE must precede the file input field -->
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
<!-- Name of input element determines name in $_FILES array -->
Send this file: <input name="userfile" type="file" />
<input type="submit" value="Send File" />
</form>
</body>
</html>
<head>
<title>文件上传</title>
</head>
<body>
<form enctype="multipart/form-data" action="up.php" method="POST">
<!-- MAX_FILE_SIZE must precede the file input field -->
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
<!-- Name of input element determines name in $_FILES array -->
Send this file: <input name="userfile" type="file" />
<input type="submit" value="Send File" />
</form>
</body>
</html>
2 文件上传接收代码
代码
<?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(".gif",".jpg",".png",".GIF",".JPG",".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("没有文件被上传");
}
}
$name=$_FILES['userfile']['name'];
$arr=explode('.',$name);
$ext=".".$arr[count($arr)-1];
$reftime = localtime(time(), true);
$prftime = sprintf("%s-%s-%s-%s-%s-%s",
$reftime['tm_year']+1900,
$reftime['tm_mon']+1,
$reftime['tm_mday'],
$reftime['tm_hour'],
$reftime['tm_min'],
$reftime['tm_sec']
);
$path=$_SERVER['DOCUMENT_ROOT']."/uploads/";
$tarName=$prftime.$ext;
$c=uploadfile($_FILES['userfile']['type'],$_FILES['userfile']['name'],$ext,$_FILES['userfile']['size'],$_FILES['userfile']['error'],$_FILES['userfile']['tmp_name'],$tarName,$path);
if($c==0)
{
echo "文件上传成功";
}
else
{
echo ("文件上传失败");
}
?>
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(".gif",".jpg",".png",".GIF",".JPG",".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("没有文件被上传");
}
}
$name=$_FILES['userfile']['name'];
$arr=explode('.',$name);
$ext=".".$arr[count($arr)-1];
$reftime = localtime(time(), true);
$prftime = sprintf("%s-%s-%s-%s-%s-%s",
$reftime['tm_year']+1900,
$reftime['tm_mon']+1,
$reftime['tm_mday'],
$reftime['tm_hour'],
$reftime['tm_min'],
$reftime['tm_sec']
);
$path=$_SERVER['DOCUMENT_ROOT']."/uploads/";
$tarName=$prftime.$ext;
$c=uploadfile($_FILES['userfile']['type'],$_FILES['userfile']['name'],$ext,$_FILES['userfile']['size'],$_FILES['userfile']['error'],$_FILES['userfile']['tmp_name'],$tarName,$path);
if($c==0)
{
echo "文件上传成功";
}
else
{
echo ("文件上传失败");
}
?>