PHP中文件上传zip与linux常用命令

PHP中文件上传zip与linux常用命令

    在PHP中通过web页上传一个文件不是什么难事,上传多个(十个以上)小文件,
  也可以延伸一个文件上传在循环达到目的。可是这样不但页面难看,操作也极其
  繁琐。但我们可以利用php和linux的完美结合来解决这个问题!
     思路:打包小文件,上传后解压.文件可用copy命令拷到目的目录
     说明:如果资料太大,须更改php设置,注意目录属性.
     
  upload.php

  <html>
  <body>
<?
  
if(!empty($submit)){
    if(
$data_file!='none'){
      
mkdir("temp",0777);//建立目录 
      
chmod("temp",0777);//改变属性 
      
if($data_type=='zip'){
        
copy($data_file,"temp/temp.zip");
        
exec("unzip -o temp/temp.zip -d temp/");//解压zip
        
exec("rm -f temp/temp.zip");//删除
      
}else{
        
copy($data_file,"temp/temp.tar.gz");//解压tar.gz
        
chdir("temp");//更改执行目录
        
exec("tar zxvf temp.tar.gz");//执行目录在当前,不需路经    
        
exec("rm -f temp.tar.gz"); //删除        
        
chdir("../");
      }
      
$files=dir("temp");
      echo 
"上传资料包包含文件<br>";
      
$i=0;
      while(
$entry=$files->read()){
         if(
$i>1){
           
$flie_name[$i]=$entry;
           
$flie_size[$i]=ceil(filesize("temp/$entry")/1000);
           echo 
$flie_name[$i]."(".$flie_size[$i].")k";
         }
        
$i++;
      }
      
$files->close();
    }
  }else{
?>
  <form method="post" action="<?=$PHP_SELF;?>
  enctype="multipart/form-data">
  <table width="90%" border="1" align="center">
  <tr> 
  <td width="26%" align="right" bgcolor="#DBDBDB">资料包类别</td>
  <td width="74%" bgcolor="#EFEFEF"> 
  <input type="radio" name="data_type"  size="20">tar.gz&nbsp;
  <input type="radio" name="data_type"  size="20">zip
  </td>
  </tr>
  <tr> 
  <td width="26%" align="right" bgcolor="#DBDBDB">资料包</td>
  <td width="74%" bgcolor="#EFEFEF"> 
  <input type="file" name="data_file"  size="20">&nbsp;
  </td>
  </tr>
  <tr align="center"> 
  <td colspan="2"> 
  <input type="submit" name="submit" value="确定">
  <input type="reset" value="取消">
  </td>
  </tr>
  </table>
<?
  
}
?>
  </body>
  </html>