addFromString() Method of ZipArchive

addFromString() Method of ZipArchive


Posted in : PHP Posted on : January 29, 2011 at 6:41 PM Comments : [ 0 ]

This section contains the detail about the addFromString() Method of ZipArchive.

addFromString() Method of ZipArchive

In this section, you will learn how to add a file to a ZIP archive using its contents.

Syntax of the  addFromString() method is :

bool ZipArchive::addFromString ( string localname, string contents )

The localname is the name of the file which you want to create or add the content in it.

The content should be replaced in syntax by the content you want to add to the above mention file.

Example :

<?php
$zip = new ZipArchive;
$res = $zip->open('test.zip', ZipArchive::CREATE);
if ($res === TRUE) {
$zip->addFromString('dir/test.txt', 'Type here the content-you want to add');
$zip->close();
echo 'Content added to Zip successfully';
} else {
echo 'Failed to Add';
}
?>

In the above example, the content is added to a file which is inside the directory "dir". The output in the browser  looks like this:

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics