Blog //Announcement: BagItPHP Library

The Scholars’ Lab is pleased to announce the initial release of a PHP library implementing BagIt 0.96. BagIt is a specification from the Library of Congress for bundling and transmitting multiple files along with their meta-data. You can check out the project page at http://github.com/scholarslab/BagItPHP/.

Our work on BagItPHP stems from the open source “Omeka + Neatline” project, a collaboration of the Scholars’ Lab with the Roy Rosenzweig Center for History and New Media. “Omeka + Neatline” is supported by the Library of Congress.

Downloads

You can download the library either as a ZIP or tarball, or you can clone the repo with git:

[sourcecode language=”bash”] git clone git://github.com/scholarslab/BagItPHP [/sourcecode]

Use: Creating Bags

To create a bag, simply instantiate a new BagIt object with the name of a directory that doesn’t exist, add files to it, and package it into a tarball with the name of the bag:

[sourcecode language=”php”] require_once ‘lib/bagit.php’;

$bag = new BagIt(‘./new-directory’);

$bag->addFile(‘./exhibit/index.html’, ‘index.html’); $bag->addFile(‘./exhibit/imgs/1.png’, ‘imgs/1.png’); $bag->addFile(‘./exhibit/imgs/2.png’, ‘imgs/2.png’);

$bag->package(‘./new-directory’); // The bag package will be created named ./new-directory.tgz. [/sourcecode]

Use: Reading Bags

To read a bag, simply open an existing back, validate it (optional), fetch remote resources, and iterate over the files, copying them or processing them in some other way.

[sourcecode language=”php”] require_once ‘lib/bagit.php’;

$bag = new BagIt(‘./existing-bag.zip’);

$bag->validate(); if (count($bag->getBagErrors()) == 0) { $bag->fetch->download();

foreach ($bag->getBagContents() as $filename) {
    copy($filename, 'final/destination/' . basename($filename));
} }

[/sourcecode]

For more information about the methods that are available, please see the documentation.

Let Us Hear from You

If you’re using this library or have any feedback on it, we’d love to hear from you! We are relying on the GitHub issues tracker for code feedback, so you can file bugs or other issues there. If you have a more general question, feel free to post here.

Cite this post: Eric Rochester. “Announcement: BagItPHP Library”. Published April 19, 2011. http://scholarslab.org//blog/announcement-bagitphp-library/. Accessed on .