一个文件系统可以解压哦

widuu 2012-9-20 PHP技术 4,600 2 喜欢 (0)

公司空间是一个大的空间,我们都打包上传,需要解压缩,我就写了一个文件关系的小系统,下边是代码
[php]
<?php
/*
*在线文件系统 在线编辑仅支持utf-8
* 作者 肖伟 powered by Widuu
*
*/
$path = "./";
$filelist=array("filesystem.php");//要过滤掉的文件

//一、根据action的信息值,做对应操作
switch($_GET['action']){
case "del": //删除一个文件
if(is_dir($_GET["filename"])){
deldir($_GET["filename"]);
}else{
unlink($_GET["filename"]);
}
echo "<br>完成.<br><br><input type='button' value='返回' onclick=\"window.location='$_SERVER[PHP_SELF]';\"><br><br>
<script language='javascript'>alert('完成.');</script>";
break;

case "create": //创建一个文件
//1.获取要创建的文件名
$filename = trim($path,"/")."/".$_POST["filename"];
//2. 判断文件是否已存在
if(file_exists($filename)){
die("要创建的文件已存在!");
}
//3. 创建这个文件
$f = fopen($filename,"w");
fclose($f);
echo "<br>完成.<br><br><input type='button' value='返回' onclick=\"window.location='$_SERVER[PHP_SELF]';\"><br><br>
<script language='javascript'>alert('完成.');</script>";
break;
echo "<br>完成.<br><br><input type='button' value='返回' onclick=\"window.location='$_SERVER[PHP_SELF]';\"><br><br>
<script language='javascript'>alert('完成.');</script>";
break;
case 'edit': //编辑文件信息
//1. 获取文件名
$filename=$_GET["filename"];
//2.读取文件的内容:
$fileinfo = file_get_contents($filename);
break;

case 'update': //执行修改文件信息
//获取信息:文件名,内容
$filename = $_POST["filename"];
$content = $_POST["content"];
//2. 执行文件内容修改
file_put_contents($filename,$content);
break;
case 'zip': //获取解压信息
//文件名和地址
$filename=$_GET["filename"];
$address=$_GET["address"];
break;
case 'unzip': //解压文件的 实列化类解压文件
$zip=new zip;
$filename=$_POST["file"];
$file=explode("./",$filename);
$address= $_POST["address"];
$result=$zip->Extract($file[1],$address);
echo "<br>完成.<br><br><input type='button' value='返回' onclick=\"window.location='$_SERVER[PHP_SELF]';\"><br><br>
<script language='javascript'>alert('完成.');</script>";
break;
}

//二、浏览指定目录下的文件,并使用表格输出

//1. path目录信息的过滤,判断path存在,并期是否是个目录
if(!file_exists($path) || !is_dir($path)){
die($path."目录无效!");
}
//2. 输出表头信息
echo "<h3>{$path}目录下的文件信息<h3>";
echo "<h4><a href='filesystem.php?action=add'>创建文件</a></h4>";
echo "<table width='600' border='0'>";
echo "<tr bgcolor='#cccccc' align='left'>";
echo "<th>序号</th><th>名称</th><th>类型</th><th>大小</th><th>创建时间</th><th>操作</th>";
echo "</tr>";

//3.打开这个目录,并遍历目录下面的所有文件
$dir = opendir($path);
if($dir){
$i=0;
//遍历目录中的文件,并输出文件的信息
while($f = readdir($dir)){
if($f=="." || $f==".." || in_array($f,$filelist)){
continue;//跳出本次循环,继续下一次遍历。
}
$file = trim($path,"/")."/".$f;
$i++;
echo "<tr>";
echo "<td>{$i}</td>";
echo "<td>{$f}</td>";
echo "<td>".type(filetype($file))."</td>";
echo "<td>".filesize($file)."</td>";
echo "<td>".date("Y-m-d H:i:s",filectime($file))."</td>";
echo "<td>
<a href='filesystem.php?filename={$file}&action=del'>删除</a>
<a href='filesystem.php?filename={$file}&action=edit'>修改</a>
<a href='filesystem.php?filename={$file}&action=zip'>解压</a>
</td>";
echo "</tr>";
}
closedir($dir); //关闭目录
}
echo "<tr bgcolor='#cccccc' align='left'><td colspan='6'>&nbsp;</td></tr>";
echo "</table>";

if($_GET['action']=="add"){
echo "<br/><br/><form action='filesystem.php?action=create' method='post'>";
echo "新建文件:<input type='text' name='filename' size='12'/> ";
echo "<input type='submit' value='新建文件'/>";

echo "</form>";
}
if($_GET['action']=="addrm"){
echo "<br/><br/><form action='filesystem.php?action=addml' method='post'>";
echo "新建目录:<input type='text' name='filename' size='12'/> ";
echo "<input type='submit' value='新建目录'/>";
echo "</form>";
}

if($_GET['action']=="edit"){
echo "<br/><br/><form action='filesystem.php?action=update' method='post'>";
echo "<input type='hidden' name='filename' value='{$filename}'/>";
echo "文件名:{$filename}<br/><br/>";
echo "文件内容:<textarea name='content' cols='40' rows='6'>{$fileinfo}</textarea><br/><br/>";
echo "<input type='submit' value='执行编辑'/>";

echo "</form>";
}
if($_GET['action']=="zip"){
echo "<br/><br/><form action='filesystem.php?action=unzip' method='post'>";
echo "<input type='hidden' name='file' value='{$filename}'/>";
echo "文件名:{$filename}<br/><br/>";
echo "解压地址:<input type='text' name='address' ></input><br/><br/>";
echo "<input type='submit' value='解压'/>";
echo "</form>";
}
//删除目录的方法 如果是文件直接删除
function deldir($dirname){
$dir=opendir($dirname);
while($filename=readdir($dir)){
if($filename!="." && $filename!=".."){
$file=$dirname."/".$filename;
if(is_dir($file)){
deldir($file);
}else{
unlink($file);
}
}
}
rmdir($dirname);
echo "删除".$dirname."成功";
closedir($dir); //关闭目录
}
//显示是目录还是文件 判断
function type($name){
switch ($name){
case 'file':
$name="文件";
return $name;
break;
case 'dir':
$name="目录";
return $name;
break;
}
return $name;
}
//解压缩的类的方法
class zip
{

var $total_files = 0;
var $total_folders = 0;

function Extract ( $zn, $to, $index = Array(-1) )
{
$ok = 0; $zip = @fopen($zn,'rb');
if(!$zip) return(-1);
$cdir = $this->ReadCentralDir($zip,$zn);
$pos_entry = $cdir['offset'];

if(!is_array($index)){ $index = array($index); }
for($i=0; $index[$i];$i++){
if(intval($index[$i])!=$index[$i]||$index[$i]>$cdir['entries'])
return(-1);
}
for ($i=0; $i<$cdir['entries']; $i++)
{
@fseek($zip, $pos_entry);
$header = $this->ReadCentralFileHeaders($zip);
$header['index'] = $i; $pos_entry = ftell($zip);
@rewind($zip); fseek($zip, $header['offset']);
if(in_array("-1",$index)||in_array($i,$index))
$stat[$header['filename']]=$this->ExtractFile($header, $to, $zip);
}
fclose($zip);
return $stat;
}

function ReadFileHeader($zip)
{
$binary_data = fread($zip, 30);
$data = unpack('vchk/vid/vversion/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len', $binary_data);

$header['filename'] = fread($zip, $data['filename_len']);
if ($data['extra_len'] != 0) {
$header['extra'] = fread($zip, $data['extra_len']);
} else { $header['extra'] = ''; }

$header['compression'] = $data['compression'];$header['size'] = $data['size'];
$header['compressed_size'] = $data['compressed_size'];
$header['crc'] = $data['crc']; $header['flag'] = $data['flag'];
$header['mdate'] = $data['mdate'];$header['mtime'] = $data['mtime'];

if ($header['mdate'] && $header['mtime']){
$hour=($header['mtime']&0xF800)>>11;$minute=($header['mtime']&0x07E0)>>5;
$seconde=($header['mtime']&0x001F)*2;$year=(($header['mdate']&0xFE00)>>9)+1980;
$month=($header['mdate']&0x01E0)>>5;$day=$header['mdate']&0x001F;
$header['mtime'] = mktime($hour, $minute, $seconde, $month, $day, $year);
}else{$header['mtime'] = time();}

$header['stored_filename'] = $header['filename'];
$header['status'] = "ok";
return $header;
}

function ReadCentralFileHeaders($zip){
$binary_data = fread($zip, 46);
$header = unpack('vchkid/vid/vversion/vversion_extracted/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len/vcomment_len/vdisk/vinternal/Vexternal/Voffset', $binary_data);

if ($header['filename_len'] != 0)
$header['filename'] = fread($zip,$header['filename_len']);
else $header['filename'] = '';

if ($header['extra_len'] != 0)
$header['extra'] = fread($zip, $header['extra_len']);
else $header['extra'] = '';

if ($header['comment_len'] != 0)
$header['comment'] = fread($zip, $header['comment_len']);
else $header['comment'] = '';

if ($header['mdate'] && $header['mtime'])
{
$hour = ($header['mtime'] & 0xF800) >> 11;
$minute = ($header['mtime'] & 0x07E0) >> 5;
$seconde = ($header['mtime'] & 0x001F)*2;
$year = (($header['mdate'] & 0xFE00) >> 9) + 1980;
$month = ($header['mdate'] & 0x01E0) >> 5;
$day = $header['mdate'] & 0x001F;
$header['mtime'] = mktime($hour, $minute, $seconde, $month, $day, $year);
} else {
$header['mtime'] = time();
}
$header['stored_filename'] = $header['filename'];
$header['status'] = 'ok';
if (substr($header['filename'], -1) == '/')
$header['external'] = 0x41FF0010;
return $header;
}

function ReadCentralDir($zip,$zip_name){
$size = filesize($zip_name);

if ($size < 277) $maximum_size = $size;
else $maximum_size=277;

@fseek($zip, $size-$maximum_size);
$pos = ftell($zip); $bytes = 0x00000000;

while ($pos < $size){
$byte = @fread($zip, 1); $bytes=($bytes << 8) | ord($byte);
if ($bytes == 0x504b0506 or $bytes == 0x2e706870504b0506){ $pos++;break;} $pos++;
}

$fdata=fread($zip,18);

$data=@unpack('vdisk/vdisk_start/vdisk_entries/ventries/Vsize/Voffset/vcomment_size',$fdata);

if ($data['comment_size'] != 0) $centd['comment'] = fread($zip, $data['comment_size']);
else $centd['comment'] = ''; $centd['entries'] = $data['entries'];
$centd['disk_entries'] = $data['disk_entries'];
$centd['offset'] = $data['offset'];$centd['disk_start'] = $data['disk_start'];
$centd['size'] = $data['size']; $centd['disk'] = $data['disk'];
return $centd;
}

function ExtractFile($header,$to,$zip){
$header = $this->readfileheader($zip);

if(substr($to,-1)!="/") $to.="/";
if($to=='./') $to = '';
$pth = explode("/",$to.$header['filename']);
$mydir = '';
for($i=0;$i<count($pth)-1;$i++){
if(!$pth[$i]) continue;
$mydir .= $pth[$i]."/";
if((!is_dir($mydir) && @mkdir($mydir,0777)) || (($mydir==$to.$header['filename'] || ($mydir==$to && $this->total_folders==0)) && is_dir($mydir)) ){
@chmod($mydir,0777);
$this->total_folders ++;
echo "<input name='dfile[]' type='checkbox' value='$mydir' checked> <a href='$mydir' target='_blank'>目录: $mydir</a><br>";
}
}

if(strrchr($header['filename'],'/')=='/') return;

if (!($header['external']==0x41FF0010)&&!($header['external']==16)){
if ($header['compression']==0){
$fp = @fopen($to.$header['filename'], 'wb');
if(!$fp) return(-1);
$size = $header['compressed_size'];

while ($size != 0){
$read_size = ($size < 2048 ? $size : 2048);
$buffer = fread($zip, $read_size);
$binary_data = pack('a'.$read_size, $buffer);
@fwrite($fp, $binary_data, $read_size);
$size -= $read_size;
}
fclose($fp);
touch($to.$header['filename'], $header['mtime']);
}else{
$fp = @fopen($to.$header['filename'].'.gz','wb');
if(!$fp) return(-1);
$binary_data = pack('va1a1Va1a1', 0x8b1f, Chr($header['compression']),
Chr(0x00), time(), Chr(0x00), Chr(3));

fwrite($fp, $binary_data, 10);
$size = $header['compressed_size'];

while ($size != 0){
$read_size = ($size < 1024 ? $size : 1024);
$buffer = fread($zip, $read_size);
$binary_data = pack('a'.$read_size, $buffer);
@fwrite($fp, $binary_data, $read_size);
$size -= $read_size;
}

$binary_data = pack('VV', $header['crc'], $header['size']);
fwrite($fp, $binary_data,8); fclose($fp);

$gzp = @gzopen($to.$header['filename'].'.gz','rb') or die("Cette archive est compress閑");
if(!$gzp) return(-2);
$fp = @fopen($to.$header['filename'],'wb');
if(!$fp) return(-1);
$size = $header['size'];

while ($size != 0){
$read_size = ($size < 2048 ? $size : 2048);
$buffer = gzread($gzp, $read_size);
$binary_data = pack('a'.$read_size, $buffer);
@fwrite($fp, $binary_data, $read_size);
$size -= $read_size;
}
fclose($fp); gzclose($gzp);

touch($to.$header['filename'], $header['mtime']);
@unlink($to.$header['filename'].'.gz');

}
}

$this->total_files ++;
echo "<input name='dfile[]' type='checkbox' value='$to$header[filename]' checked> <a href='$to$header[filename]' target='_blank'>文件: $to$header[filename]</a><br>";

return true;
}

// end class
}

[/php]
这里边除了zip类也许大家不太明白,估计其他的大家应该都懂吧!如果您有什么不懂尽管给我留言!欢迎转载,转载请注明来自微度网络http://www.widuu.com

转载请注明来自微度网络,本文标题:《一个文件系统可以解压哦》

喜欢 0 发布评论
Top