Import Binary Files

Author Message

Oliver Schrenk

Monday 18 August 2008 7:14:15 am

Im trying to write a short command line script to import pdf-documents located in a folder on my hard drive, but I have some problems working through the documentation

This is what i came up with:

function import ($nodeID, $filePath, $name) { 
		if ( file_exists( $filePath ) )
		{
			$classID = 28;	// file class id
			$creatorID = 14;	// user object id > default administrator
			
			$class =& eZContentClass::fetch( $classID );
			$parentNodeID = $nodeID;
			$contentObject =& $class->instantiate( $creatorID, 1 );	

			$nodeAssignment =& eZNodeAssignment::create(
				 array(
					'contentobject_id' => $contentObject->attribute( 'id' ),
					'contentobject_version' => $contentObject->attribute( 'current_version' ),
					'parent_node' => $parentNodeID,
					'sort_field' => 2,
					'sort_order' => 0,
					'is_main' => 1
				));
			$nodeAssignment->store();

			$version =& $contentObject->version( 1 );
			$version->setAttribute( 'modified', eZDateTime::currentTimeStamp() );
			$version->setAttribute( 'status', EZ_VERSION_STATUS_DRAFT );
			$version->store();

			$contentObjectID = $contentObject->attribute( 'id' );
			$dataMap =& $contentObject->dataMap();

			$dataMap['name']->setAttribute( 'data_text', $name );
			$dataMap['name']->store();

			$fileContent =& $dataMap['binary']->attribute( 'content' );
			$fileContent->initializeFromFile( $filePath , $name);
			$dataMap['file']->store();

 			$operationResult = eZOperationHandler::execute (
				'content',
				'publish',
				 array (
					'object_id' => $contentObjectID,
					'version' => 1
				 ));
		}
	}

I get an error message like

Call to a member function initializeFromFile() on a non-object in

I guess the datamap is wrong, but I don't know how to set it. Any hints?

Gabriel Finkelstein

Tuesday 19 August 2008 10:57:49 am

Is your file attribute called "binary" or "file"?

Take a look at these extensions, maybe they help.
http://ez.no/developer/contribs/import_export/xmlimport
http://zev.ez.no/svn/extensions/csv/trunk/

Oliver Schrenk

Tuesday 19 August 2008 12:51:36 pm

The attribute is "file". Thanks for the link. The xml import contribution almost answers all my questions.

The API is weird. Why is there a helper function to initialize images but not for files?

Gabriel Finkelstein

Tuesday 19 August 2008 3:02:03 pm

In this line you use "binary". I suppose it should be "file".

$fileContent =& $dataMap['binary']->attribute( 'content' );

But still, I guess it won't work since, as you say, there's no such method as initializeFromFile.

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