Jump to content

[SOLVED] ZipArchive


ballouta

Recommended Posts

Hello

 

I am using this code to read a protected directory (username&password) contents called (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.

So if open the compressed file i get: 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)

 

 

The path that I wish to place my code file in is:

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?

 

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). Can I add only the content of protect directory in the Zip Archive without inclduing the directory itself?

 

Many thanks

Link to comment
Share on other sites

 

bool ZipArchive::addFile  ( string $filename  [, string $localname  ] )

  Adds a file to a ZIP archive from a given path

Parameters

  filename - The path to the file to add.

  localname - local name inside ZIP archive.

 

 

So replace $zip->addFile(.....) with the following, which will remove everything up to and including the first instance of "protect" in the zip file paths.

 

$realFileName = realpath($key);
$zipFileName = substr($realFileName,strpos($realFileName,"protect")+7);
$zip->addFile($realFileName,$zipFileName) or die ("ERROR: Could not add file: $key");

 

This will let you solve both problems.

 

 

Link to comment
Share on other sites

Many thanks, the files names are correct now.

 

I have another type of problem, I edited the same code to copy the files to another directory, but i am getting this error:

 

Warning: copy(/home/simwsim/public_html/submitted/980995) [function.copy]: failed to open stream: Is a directory in /home/simwsim/public_html/admin/files/index2.php on line 42

failed to copy 123456789123456.ask...

 

<?php

require_once("../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 a directory to copy the compressed files to
mkdir("/home/simwsim/public_html/submitted/$random", 0777);

// 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("/home/simwsim/public_html/protect/"));

// iterate over the directory
// add each file found to the archive
foreach ($iterator as $key=>$value) {
	if ($key != '/home/simwsim/public_html/protect/.htaccess')
			{
		$remove = array("/home/simwsim/public_html/protect/");
		$replace = array("");
		$file = str_replace($remove, $replace, $key);

$realFileName = realpath($key);
$zipFileName = substr($realFileName,strpos($realFileName,"protect")+;
$zip->addFile($realFileName,$zipFileName) or die ("ERROR: Could not add file: $key");

		//Move the file to the submitted directory
		if (!copy("/home/simwsim/public_html/protect/$file", "/home/simwsim/public_html/submitted/".$random)) { echo "failed to copy $file...\n"; }
			} else {}
	}//foreach

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

 

Note that the submitted directory already exists.

where is the problem? :(

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.