2012-03-27 47 views
1

我试图从一个数组中构建一个具有特定文件的目录树,并将它推送下载。这部分工作正常。PHP压缩函数

问题是当我尝试填充内容的特定文件。

下载完成后,文件为空。

但是,switch case语句被执行并且所有的方法都在工作。

我认为问题在于我的zip函数,但我不知道我应该搜索什么(或者代码设计不是最好的)。

<?php 
include_once 'class.file.php'; 

class CreateComponent{ 



    function __construct(){ 

     if (isset($_POST['name'])){ 

     $this->createDirectoryTree($_POST['name']); 

     } 

     else{ 

      echo '<h1>error!</h1>'; 

     } 

    } 

    function createDirectoryTree($component_name){ 

     $component_name="com_".$component_name; 

     $this->create_zip($component_name); 

    } 

    function create_zip($component) { 


     $jcomp=str_replace("com_", "", $component); 

     $dirs= array(
       $jcomp.'.xml', 
       'site/'.$jcomp.'.php', 
       'site/views/index.html', 
       'site/views/to_change/index.html', 
       'site/views/to_change/tmpl/index.html', 
       'site/views/to_change/tmpl/default.xml', 
       'site/views/to_change/tmpl/default.php', 
       'site/models/index.html', 
       'site/controllers/index.html', 
       'site/tables/index.html', 
       'site/index.html', 
       'admin/index.html', 
       'admin/views/index.html', 
       'admin/models/index.html', 
       'admin/controllers/index.html', 
       'admin/sql/updates/index.html', 
       'admin/sql/updates/mysql/0_1.sql' 
     ); 

     $file = tempnam("tmp", "zip"); 

     $zip = new ZipArchive(); 

     foreach ($dirs as $dir){ 
      $zip->open($file, ZIPARCHIVE::CREATE); 
      $zip->addEmptyDir($dir); 


     switch ($dir){ 

      case (substr($dir, strrpos($dir, '/')+1) == "index.html"): 

      $zip->addFromString($dir, '<html><body bgcolor="#FFFFFF"></body></html>'); 
      break; 

      case "site/$jcomp.php": 
      $main_php= new File(); 
//   echo $main_php->main_controller($jcomp); 
      $zip->addFromString($dir,$main_php->main_controller($jcomp)); 

      $zip->addFromString($dir, ''); 
      break; 

      case "$jcomp.xml": 
      $main_php= new File(); 
      $zip->addFromString($dir,$main_php->main_xml($jcomp)); 

      break; 


} 


     $zip->addFromString($dir, ''); 


     } 
     $zip->close(); 



     // Stream the file to the client 
     header('Content-Type: application/zip'); 
     header('Content-Length: ' . filesize($file)); 
     header('Content-Disposition: attachment; filename= "'.$component.'.zip"'); 
     readfile($file); 
     unlink($file); 
    } 



} 

$component= new CreateComponent(); 

回答

1

乍一看,我会改变以下行

$zip = new ZipArchive(); 

    foreach ($dirs as $dir){ 
     $zip->open($file, ZIPARCHIVE::CREATE); 
     $zip->addEmptyDir($dir); 

$zip = new ZipArchive(); 
    $zip->open($file, ZIPARCHIVE::CREATE); 

    foreach ($dirs as $dir){ 
     $zip->addEmptyDir($dir); 

为你打开的文件,每次你添加一个目录,似乎我错了。

另一方面,我会设置一个组件名称策略并在构造函数中检查它以防止输入使用。

我不知道,也许是一个好名字的政策是 - 只是字母字符,数字和连允许进入一个模块名称 -

+0

感谢您的答复和说明!不幸的是,改变上面的行似乎没有任何影响。 – lgt 2012-03-27 15:31:36