php中的文件上传很简单 不像java 中需要借助 commons-fileupload-1.2.1.jar 等第三个的上转工具来做,下面我写了一个简单的上转类。
php中$_FILES内置函数包含了要上传文件的所有信息量。
$_FILES("uploadName"); 输出的话可以看见他是一个数组里面包含了 name,type,tmp_name,error,size元素
上传不可避免,要出现异常,php为此给出了5个类型常
0---> 文件上转成功 --->UPLOAD_ERR_OK
1--->上传文件超过php.ini 的upload_max_file的设置--->UPLOAD_ERR_INI_SIZE
2--->上传文件超过特殊表单项MAX_FILE_SIZE(我们自己设置的)设置的值--->UPLOAD_ERR_FORM_SIZE
3---> 文件只有部分上转-----> UPLOAD_ERR_PARTLAY
4---> 没有文件被上转-----> UPLOAD_NO_FILE
一个简单的上传
<?php
/******
* @author wangyalei
* 上传公用类
*
*/
class upload {
//上传的类型
private $type = array();
//上传的保存路径
private $savepath;
//上传的大小
private $size;
//上传内容信息
private $uploadinfo;
private $label_name;
/******
* @param $type 可以是数组 也可是 多个字符串(字符串,字符串)
* @param $savepath 上传后保存的路径
* @param $size 设定上传的大小
*
*/
public function __construct($type,$savepath,$size,$label_name){
$this->type = is_array($type)?$this->type = $type:$this->type = implode(",",$type);
$this->savepath = $savepath;
$this->size = $size;
$this->label_name = $label_name;
$this->uploadinfo = $_FILES[$label_name];
}
//保存
public function save(){
//上传要进行检测是否合法
//1 判断用户是真实上传
if(!is_uploaded_file($this->uploadinfo["tmp_name"])){
echo "文件不存在";
exit;
}
//2判断类型是否正确
if(!in_array($this->uploadinfo["type"],$this->type)){
echo "<font color='red'>文件类型不符!只能上传".implode(',',$this->type)."类型</font>";
exit;
}
//3判断上传大小
if($this->size<$this->uploadinfo["size"]){
echo "文件太大!";
exit;
}
//2 判断是否有要保持的文件夹 如没有创建一个
if(!file_exists($this->savepath)){
mkdir($this->savepath);
}
//从临时目录移除 并给文件重命名 防止 重名覆盖
if(!move_uploaded_file($this->uploadinfo["tmp_name"],$this->savepath."/".$this->uploadinfo["name"])){
echo $this->uploadinfo["tmp_name"];
echo "移动文件出错";
exit;
}
//最后判断一点错都没有
if($this->uploadinfo["error"]=="0"){
echo "上传成功";
}
}
}
?>
测试一下:
<?php
/*
* Created on 2010-8-19
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
*/
include("upload.class.php");
//上传文件类型列表
$uptypes=array(
'image/jpg',
'image/jpeg',
'image/png',
'image/pjpeg',
'image/gif',
'image/bmp',
'image/x-png'
);
$max_file_size=2000000*1024; //上传文件大小限制, 单位BYTE
$destination_folder="uploadimg"; //上传文件路径
$upload = new upload($uptypes,$destination_folder,$max_file_size,"file1");
$upload->save();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>上转文件</title>
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="file1"><span id="upload"></span><br/>
<input type="submit" name="submit" value="上传">
</form>
</body>
</html>
以上是上传,下面是下载
<?php
function download($url, $filename) {
// 获得文件大小, 防止超过2G的文件, 用sprintf来读 %u 十进制无符号整数
$filesize = sprintf ( "%u", filesize($url ) );
//spinrtf
if (!$filesize) {
return;
}
header ( "Content-type:application/octet-stream\n" ); //application/octet-stream
header ( "Content-type:unknown/unknown;" );
header ( "Content-disposition: attachment; filename=\"" . $filename . "\"" );
header ( 'Content-transfer-encoding: binary' );
if ($range = getenv ( 'HTTP_RANGE' )) { // 当有偏移量的时候,采用206的断点续传头
$range = explode ( '=', $range );
$range = $range [1];
//
header ( "HTTP/1.1 206 Partial Content" );
header ( "Date: " . gmdate ( "D, d M Y H:i:s" ) . " GMT" );
header ( "Last-Modified: " . gmdate ( "D, d M Y H:i:s", filemtime ( $url ) ) . " GMT" );
header ( "Accept-Ranges: bytes" );
header ( "Content-Length:" . ($filesize - $range) );
header ( "Content-Range: bytes " . $range . ($filesize - 1) . "/" . $filesize );
header ( "Connection: close" . "\n\n" );
} else {
header ( "Content-Length:" . $filesize . "\n\n" );
$range = 0;
}
loadFile ( $url );
}
function loadFile($filename, $retbytes = true) {
$buffer = '';
$cnt = 0;
$handle = fopen ( $filename, 'rb' );
if ($handle === false) {
return false;
}
while ( ! feof ( $handle ) ) {
$buffer = fread ( $handle, 1024 * 1024 );
echo $buffer;
ob_flush ();
flush ();
if ($retbytes) {
$cnt += strlen ( $buffer );
}
}
$status = fclose ( $handle );
if ($retbytes && $status) {
return $cnt; // return num. bytes delivered like readfile() does.
}
return $status;
}
?>
测试代码
<?php
include("downupload.php");
download($_GET[url], $_GET[name]);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="index.php?url=index.php&name=我的.txt">下载</a>
</body>
</html>