【问题标题】:How to extract rar/archive files and folders using php in windows?如何在 Windows 中使用 php 提取 rar/存档文件和文件夹?
【发布时间】:2013-01-28 05:04:02
【问题描述】:

我喜欢使用 php 提取 rar/archive 文件和文件夹。 但我尝试过使用这个脚本

<?php 
    $rar_file = rar_open('example.rar') or die("Can't open Rar archive");
    $entries = rar_list($rar_file); 
    foreach ($entries as $entry) { 
        echo 'Filename: ' . $entry->getName() . "\n"; 
        $entry->extract('/dir/extract/to/'); 
    } 
    rar_close($rar_file); 
?>

【问题讨论】:

    标签: php


    【解决方案1】:

    你正在使用的功能需要你先install它。

    您可能希望使用exec() 调用命令行工具而不是它,但请确保您保护自己免受shell 注入。

    【讨论】:

      【解决方案2】:
      $archive_name = '/full/path/to/file.rar'
      $entry_name = 'path/to/archive/entry.txt'; //notice: no slash at the beginning
      $dir_to_extract_to = '/path/to/extract/dir';
      $new_entry_name = 'some.txt';
      
      
      $rar = rar_open($archive_name) OR die('failed to open ' . $archive_name);
      $entry = rar_entry_get($rar, $entry_name) OR die('failed to find ' . $entry_name . ' in ' . $archive_name);
      
      // this will create all necessary subdirs under $dir_to_extract_to
      $entry->extract($dir_to_extract_to); 
      /* OR */
      
      // this will create only one new file $new_entry_name in $dir_to_extract_to
      $entry->extract('', $dir_to_extract_to.'/'.$new_entry_name); 
      
      // this line is really not necessary
      rar_close($rar);
      

      【讨论】:

      • 是的,您需要在您的服务器上安装数据包pecl.php.net/package/rar 轻松安装,您可以使用这种方式 $rar = rar_open($uploadfile) OR die('failed to open ' . $uploadfile); $list = rar_list($rar); foreach($list as $file) { $entry = rar_entry_get($rar, $file->getName()); $entry->extract("."); $uploadfile = Upload_DIR.$file->getName(); rar_close($rar); }
      猜你喜欢
      • 2017-07-14
      • 2018-10-29
      • 2017-09-19
      • 2016-08-06
      • 1970-01-01
      • 1970-01-01
      • 2017-09-17
      • 2018-05-20
      • 2011-02-07
      相关资源
      最近更新 更多