eZ Community » Forums » Developer » Extract an eZBinaryFile from Cluster
expandshrink

Extract an eZBinaryFile from Cluster

Extract an eZBinaryFile from Cluster

Friday 02 April 2010 4:33:31 pm - 2 replies

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.

Saturday 03 April 2010 11: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 ?

Tuesday 06 April 2010 4:42:29 pm

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.

expandshrink

You must be logged in to post messages in this topic!

36 542 Users on board!

Forums menu

Proudly Developed with from