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

PHP文件上传类及其使用实例教程

2012-03-11 21:24 671 查看
这个例子主要介绍了一个比较通用的文件上传类。当我们实例化这个类的时候可以指定上传路径和允许上传的文件类型。

本例包含两个文件

upload.class.php

test.php

上传类中的属性和方法列表如下图所示:

$path:文件上传路径

$type:允许上传的文件类型

set_type():设置可以上传的文件类型

up():上传处理

Name():得到文件名

_file_type():得到文件类型

_make_dir():创建目录

GetPath():得到路径



以下是upload.class.php源代码:

path = $path;
//更新文件类型
$this->type = $this->set_type($type);

}

function set_type($type = "png,jpg,gif,rar,zip,gz") {
if (!$type) {
$type = "png,jpg,gif,rar,zip,gz";
}
$type_array = explode(",", $type);
$array = array ();
foreach ($type_array as $key => $value) {
$value = trim($value);
if (strlen($value) > 1) {
if ((substr($value, 0, 1) != ".")) {
$value = "." . $value;
}
$array[$key] = $value;
}
}
$this->type = implode(",", $array);
$mytype = $this->type;
return $mytype;
}

function up($var, $file = "") {
if (empty ($var)) {
return false;
}
$this->_make_dir($this->path); #[更新附件路径]
$file_name = $this->_check($file);
if (!$file_name)
$file_name = time(); //如果文件名为空,刚使用时间作为文件名称
//检查文件名称是否含有后缀,有则去掉
$file_name = strtolower($file_name); //将所有大写改为小写
//-----
$file_type = $this->_file_type($var);
if ($file_type) {
if (strpos($file_name, ".") === false) {
$filename = $file_name . $file_type; //新的文件名
} else {
$filename = $file_name;
}
#[由于PHP不支持客户端检查文件大小,固这里没有对文件大小进行限制]
#[在客户端对上传进行大小限制!]
$up = copy($_FILES[$var]["tmp_name"], $this->path . $filename);
if ($up) {
return $filename;
} else {
return false;
}
} else {
return false;
}
}

function Name($var) {
return $_FILES[$var]["name"];
}

function _file_type($var) {
if ($_FILES[$var]["name"]) {
$name = explode(".", $_FILES[$var]["name"]);
$count = count($name);
$type = "." . strtolower($name[$count -1]);
if (strpos($this->type, $type) === false) {
return false;
}
return $type;
} else {
return false;
}
}

function _check($file = "") {
if (!$file) {
return false;
}
$file_name = basename($file);
if ($file_name == $file) {
return $file;
}
$array = explode("/", $file);
$path = "";
$count = count($array);
if ($count > 1) {
for ($i = 0; $i < ($count -1); $i++) {
$path .= $array[$i] . "/";
}
}
$this->_make_dir($this->path . $path);
$this->path = $this->path . $path;
return $file_name;
}

#[创建目录]
function _make_dir($folder) {
$array = explode("/", $folder);
$count = count($array);
$msg = "";
for ($i = 0; $i < $count; $i++) {
$msg .= $array[$i];
if (!file_exists($msg) && ($array[$i])) {
mkdir($msg, 0777);
}
$msg .= "/";
}
return true;
}

function GetPath() {
return $this->path;
}

function FileType($filename) {
$filename = basename($filename);
$name = explode(".", $filename);
$count = count($name);
$type = strtolower($name[$count -1]);
return $type;
}
}
?>

test.php源码如下:

<html>
<head>
<meta http-equiv="Content-Type" <meta="" content="text/​html;​" charset="utf-8">
<title>文件上传</title>
</head>
<?php
include ("upload.class.php");
//实例化一个文件上传类,可以上传指定的jpg,png,rar,ppt,doc类型的文件,上传到upload目录下。
$up = new UPLOAD("upload/", "jpg,png,rar,ppt,doc");
if (isset ($_POST['submit1'])) {
$v = "upfile";

echo $up->Name("upfile");

if ($fname = $up->up($v)) {
echo "上传成功.." . $fname;
} else {
echo "上传失败";
}

} else {

echo "请选择文件上传~";
}

//print_r($_FILES['upfile']) ;
?>
<body>
<form action="" method="POST" enctype="multipart/form-data" name="form1" id="form1">
<input type="file"  name="upfile" >
<input type="submit" name="submit1" value="上传">
<p>在写测试用例的时候把全局变量$_FILES写为$_FILE,杯具哎。</p>
</form>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: