Extract an eZBinaryFile from Cluster

Author Message

Fabien Scantamburlo

Friday 02 April 2010 7:33:31 am

Hi,

I'm trying to get the content of an eZBinaryFile (previously uploaded through the admin interface) in order to use it.

I found the following code:

$fileHandler = eZBinaryFileHandler::instance();
$fileHandler->handleDownload($contentObject, $contentObjectAttribute, eZBinaryFileHandler::TYPE_FILE);

This code works fine to download the file (using the eZFilePassthroughHandler) but i can't get the file resource.

Is there any other way to extract the file from the cluster file system ?

Thanks in advance,

Fabien.

Bertrand Dunogier

Saturday 03 April 2010 2:39:27 am

Well, it depends on what you want to extract it for. If you need to read it from an extension's PHP code, then you need to fetch a temporary copy of the file. You can do this using fetch or fileFetch:

// get the eZBinaryFile's path
$filePath = $contentObjectAttribute->objectAttributeContent()->filePath();
$file = eZClusterFileHandler::instance( $filePath );

// fetch the file locally using its original name
$file->fetch();

// process the file
do_stuff_with_file( $filePath );

// delete the local copy
$file->deleteLocal();

You are then able to access the file using $filePath.
Once you're done with it, you can delete it by calling deleteLocal() on $file in order to remove the local copy.

If you only need the file's content, as binary data, you can use fetchContents() instead in order to get the contents without creating a local copy:

$filePath = $contentObjectAttribute->objectAttributeContent()->filePath();
$file = eZClusterFileHandler::instance( $filePath );

// get the file's binary content
$contents = $file->fetchContents();

do_stuff_with_file_contents( $contents );

If you are worried about concurrent operations, you can also fetch the file with a unique name so that no other process risks interacting with it:

$file = eZClusterFileHandler::instance(
    $contentObjectAttribute->objectAttributeContent()->filePath()
);

// create a local copy, and get the copy's path
$temporaryPath = $file->fetchUnique();

// process the local file
do_stuff_with_file( $temporaryPath ); 

// remove the temporary file
unlink( $temporaryPath  );

Does this help ?

Bertrand Dunogier
eZ Systems Engineering, Lyon
http://twitter.com/bdunogier
http://gplus.to/BertrandDunogier

Fabien Scantamburlo

Tuesday 06 April 2010 7:42:29 am

Hi Bertrand,

It seems to work fine ! (I only test it on the FileSystem. I'll be able to test it on the DB system in a few days. I think eZClusterFileHandler's going to be my best friend !).

Thank you very much.

Best regards,

Fabien.

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.