Tag Archives: code

PHP – Extracting a zip compressed archive into a folder

Below code sample is for extracting the contents of a .zip file into a folder given by ‘/test/’. Note that if the folder ‘test’ is not there, it is created.

$zip = new ZipArchive;
if ($zip->open($outFile) === TRUE) {
    $zip->extractTo('/test/');
    $zip->close();
    echo 'ok';
} else {
    echo 'failed';
}

The method extractTo does the process of reading all files and filenames in the archive and writing new equivalent files in the output folder. We can also selectively extract files.. for examples we want to modify the above example such that only testFile2.txt and testFile4.txt is extracted.

$zip->extractTo('/test/',array('testFile2.txt','testFile4.txt'));

PHP – Creating a ZIP compressed archive


<?
$outFile = "testBundle.zip";
// make zip archive

$zip = new ZipArchive();
$zip->open($outFile, ZIPARCHIVE::CREATE);
//add Files
$zip->addFile("testFile.txt");
$zip->addFile("testFile2.txt");
$zip->addFile("testFile3.txt");
$zip->addFile("testFile4.txt");
$zip->close();
?>

Steps in above code:
- We create an instance of ZipArchive class.
- We open (create if not present) a zip archive file by the name testBundle.zip
- We add files into our archive.. and close.
- That’s it, this would create a .zip compressed archive with the four files.

We can also add a new file to the archive with data at runtime as below.

//add File from string.. without actual file
$zip->addFromString("t1.txt", "this is a stest");

The above code creates a file t1.txt directly inside the archive with the content passed as the second parameter.

WordPress – Control When Your Posts are Available via RSS

All bloggers make errors that we catch after we publish the post. Sometimes even within the next minute or two. That is why it is best that we delay our posts to be published on the RSS by 5-10 minutes.

You can do that by adding this function:


function publish_later_on_feed($where) {
global $wpdb;

if ( is_feed() ) {
// timestamp in WP-format
$now = gmdate(‘Y-m-d H:i:s’);

// value for wait; + device
$wait = ‘10′; // integer

// http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_timestampdiff
$device = ‘MINUTE’; //MINUTE, HOUR, DAY, WEEK, MONTH, YEAR

// add SQL-sytax to default $where
$where .= ” AND TIMESTAMPDIFF($device, $wpdb->posts.post_date_gmt, ‘$now’) > $wait “;
}
return $where;
}

add_filter(‘posts_where’, ‘publish_later_on_feed’);

This code is adding a 10 minute delay on your post being shown on the RSS Feeds, you can change it by changing the number 10 to as many minutes as you like.