简体   繁体   English

在PHP中使用ZipArchive类

[英]Using ZipArchive Class in PHP

I am using this code to read a protected directory (username&password) contents called (protect). 我正在使用此代码读取称为(protect)的受保护目录(用户名和密码)内容。

<?php
require_once("admin/global.inc.php");

// increase script timeout value
ini_set('max_execution_time', 300);

//Generate a new flag
$random = (rand(000000,999999));
$date = date("y-m-d");

// create object
$zip = new ZipArchive();

// open archive 
if ($zip->open("$date-$random.zip", ZIPARCHIVE::CREATE) !== TRUE) {
    die ("Could not open archive");
}

// initialize an iterator
// pass it the directory to be processed
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator("protect/")); //check question #2

// iterate over the directory
// add each file found to the archive
foreach ($iterator as $key=>$value) {
        if ($key != 'protect/.htaccess')
                {
    $zip->addFile(realpath($key), $key) or die ("ERROR: Could not add file: $key");        
$query="INSERT INTO `archives_logs` (`id`, `file`, `flag`, `date`) VALUES (NULL, '$key', '$random', '$date')";
            $query_result = mysql_query ($query);
                }
}

// close and save archive
$zip->close();
echo "Archive created successfully.";    
?>

If I place my code file in a location differerent than the protected directory location, i have to change the path of the directory to be compressed which is fine, BUT the problem is that all directories in the path are included in the Zip Archive. 如果我将代码文件放在与受保护目录位置不同的位置,则必须更改要压缩的目录的路径,这很好,但是问题是该路径中的所有目录都包含在Zip存档中。 So if open the compressed file i get: www/username/public_html/etc... 因此,如果打开压缩文件,我会得到:www / username / public_html / etc ...

Here is the directories strcuture: www/protect/(files to be compressed here) www/compress_code.php (here is my current code file) 这是目录目录:www / protect /(此处要压缩的文件)www / compress_code.php(这是我当前的代码文件)

The path that I wish to place my code file in is: www/protect/admin/files/compress_code.php 我希望将代码文件放入的路径为:www / protect / admin / files / compress_code.php

Q1) How do I keep my code file in the last mentioned location WITHOUT including the path in my ZipArchive file? Q1)如何将我的代码文件保存在最后提到的位置,而不包括ZipArchive文件中的路径?

Q2) When my code is in the same location of the directory to be compressed, and when i open the compressed file i see, protect/(the files). Q2)当我的代码位于要压缩的目录的相同位置时,并且当我打开压缩文件时,看到的是保护/(文件)。 Can I add only the content of protect directory in the Zip Archive without inclduing the directory itself? 我可以在不包含目录本身的情况下仅在Zip存档中添加保护目录的内容吗?

It's pretty simple: 很简单:

  1. Store the target path in a variable. 将目标路径存储在变量中。
  2. Remove target path from the localname before adding the file. 在添加文件之前,从本地名称中删除目标路径。

Like this: 像这样:

$flags = FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::SKIP_DOTS;
$target = 'protect/';
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($target, $flags));

foreach ($iterator as $key=>$value) {
    if ($key != "{$target}.htaccess")
    {
        $localname = substr($key, strlen($target));
        $zip->addFile($key, $localname) or die ("ERROR: Could not add file: $key");
    }
}

This should actually answers both of your questions. 这实际上应该回答您的两个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM