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.

eZ debug

Timing: Jan 18 2025 18:27:55
Script start
Timing: Jan 18 2025 18:27:55
Module start 'layout'
Timing: Jan 18 2025 18:27:55
Module start 'content'
Timing: Jan 18 2025 18:27:56
Module end 'content'
Timing: Jan 18 2025 18:27:56
Script end

Main resources:

Total runtime1.1126 sec
Peak memory usage4,096.0000 KB
Database Queries60

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0072 587.8125152.6094
Module start 'layout' 0.00720.0034 740.421939.4141
Module start 'content' 0.01061.1004 779.8359523.6250
Module end 'content' 1.11100.0015 1,303.460912.1875
Script end 1.1125  1,315.6484 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00340.3060160.0002
Check MTime0.00130.1202160.0001
Mysql Total
Database connection0.00170.153910.0017
Mysqli_queries1.056494.9538600.0176
Looping result0.00060.0584580.0000
Template Total1.074696.620.5373
Template load0.00210.189320.0011
Template processing1.072596.397320.5362
Template load and register function0.00020.016610.0002
states
state_id_array0.00120.111310.0012
state_identifier_array0.00090.078120.0004
Override
Cache load0.00170.1553240.0001
Sytem overhead
Fetch class attribute can translate value0.00040.036820.0002
Fetch class attribute name0.00100.091550.0002
XML
Image XML parsing0.00220.201220.0011
class_abstraction
Instantiating content class attribute0.00000.001260.0000
General
dbfile0.00240.2152160.0001
String conversion0.00000.001040.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1node/view/full.tplfull/forum_topic.tplextension/sevenx/design/simple/override/templates/full/forum_topic.tplEdit templateOverride template
4content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
9content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
3content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
2content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
2content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 22
 Number of unique templates used: 7

Time used to render debug report: 0.0001 secs