How to add a file to my contentobject through PHP?

Author Message

Clemens T

Saturday 01 October 2005 3:49:50 am

Heya all, I'm useing the following code to create a contentobject/node in ezpublish:

	$class=eZContentClass::fetchByIdentifier('id_forum_post');
$parentContentObjectTreeNode = eZContentObjectTreeNode::fetch($parentNodeID);
$parentContentObject = $parentContentObjectTreeNode->attribute("object");
$sectionID = $parentContentObject->attribute( 'section_id' );
$contentObject =& $class->instantiate( $user_id, $sectionID );

$nodeAssignment =& eZNodeAssignment::create( array(
	'contentobject_id' => $contentObject->attribute('id'),
	'contentobject_version' => $contentObject->attribute('current_version' ),
	'parent_node' => $parentContentObjectTreeNode->attribute( 'node_id' ),
	'is_main' => 1
));
$nodeAssignment->store();

$contentObject->setAttribute( 'name', "ForumPost");
$contentObject->store();

$attribs =& $contentObject->contentObjectAttributes();
for($i=0;$i<count($attribs);$i++){
	switch($attribs[$i]->attribute("contentclass_attribute_identifier")) {
		case 'comment':
				$attribs[$i]->setAttribute('data_text', $http->postVariable( 'comment' ) );
				$attribs[$i]->store();
		break;
	}

Now, the question is, how can I make a File upload in the same FORM Post? I know the ezContentUpload class, but unsure on how to use it when you want to add a file attribute to your existing contentobject.

Thanks a load!
Clemens

Kristof Coomans

Saturday 01 October 2005 5:59:47 am

include_once( 'kernel/classes/datatypes/ezbinaryfile/ezbinaryfile.php' );
include_once( 'kernel/classes/lib/ezutils/ezys.php' );
include_once( 'kernel/classes/lib/ezutils/ezmimetype.php' );

$dir = eZSys::storageDirectory( ) . '/original/';

$ini =& eZINI::instance( 'site.ini' );

$mimeData =& eZMimeType::findByFileContents( $value['name'], true );

$mime = explode( '/', $mimeData['name'] );

$mimeCategory = $mime[0];

if ( !file_exists( $dir ) )
{
    include_once( 'lib/ezfile/classes/ezdir.php' );
                
    $oldumask = umask( 0 );
    if ( !eZDir::mkdir( $dir, eZDir::directoryPermission(), true ) )
    {
        umask( $oldumask );
        // TODO: abort operation
    }
    umask( $oldumask );
}

$dir .= $mimeCategory;
if ( !file_exists( $dir ) )
{
    $oldumask = umask( 0 );
    $perm = $ini->variable( 'FileSettings', 'StorageDirPermissions' );
    if ( !eZDir::mkdir( $dir, octdec( $perm ), true ) )
    {
        umask( $oldumask );
        //TODO: abort operation
    }
    umask( $oldumask );
}

$extension = preg_replace( '/.*\.(.+?)$/', '\\1', $file['name'] );
$destination = $dir . '/' . md5( $value['name'] + microtime() ) . $extension;

$fileHandle = fopen( $destination, 'wb' );
fwrite( $fileHandle, $fileContent );
fclose( $fileHandle );

$perm = $ini->variable( 'FileSettings', 'StorageFilePermissions' );
$oldumask = umask( 0 );
chmod( $destination, octdec( $perm ) );
umask( $oldumask );

// Write log message to storage.log
include_once( 'lib/ezutils/classes/ezlog.php' );
$storageDir = $dir . '/';
eZLog::writeStorageLog( $value['name'], $storageDir );

$binary =& eZBinaryFile::create( $attrib->attribute( 'id' ), 1 );

$binary->setAttribute( 'contentobject_attribute_id', $attrib->attribute( 'id' ) );
$binary->setAttribute( 'version', $attrib->attribute( 'version' ) );
$binary->setAttribute( 'filename', basename( $destination ) );
$binary->setAttribute( 'original_filename', $value['name'] );
$binary->setAttribute( 'mime_type', $mimeData['name'] );

$binary->store( );

$attribs[$i]->setContent( $binary );
$attribs[$i]->store();

where $fileContent is the content of your file.

independent eZ Publish developer and service provider | http://blog.coomanskristof.be | http://ezpedia.org

Clemens T

Saturday 01 October 2005 6:04:18 am

Ok, this helps a lot.

But what is:
$value['name'] ? and the value of $attrib remains unknown?

Thanks,
Clemens

Clemens T

Monday 03 October 2005 4:35:56 am

Ok, dicussion has been continued here:

http://ez.no/community/forum/developer/what_s_wrong_with_this_code

Kristof Coomans

Monday 03 October 2005 4:58:24 am

Sorry, I forgot to replace some variables in my code. I'm working on a custom API which makes importing objects easier, this was a code sample from it.

Just replace
- $attrib with $attribs[$i].
- $value['name'] with $fileName, where $fileName is the name of your file.

independent eZ Publish developer and service provider | http://blog.coomanskristof.be | http://ezpedia.org

Kristof Coomans

Monday 03 October 2005 5:00:36 am

When I say 'importing', I actually mean creating objects in PHP code.

Maybe we can work on it together? As I see you are doing almost the same. You can contact me at kristof[dot]coomans[at]sckcen[dot]be

independent eZ Publish developer and service provider | http://blog.coomanskristof.be | http://ezpedia.org

Clemens T

Tuesday 04 October 2005 5:30:30 am

I'll contact you in about a week, I'm currently @ a deadline for my job...so not much time left!
Thanks for your input on all my posts :).
Clemens

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 31 2025 07:38:15
Script start
Timing: Jan 31 2025 07:38:15
Module start 'layout'
Timing: Jan 31 2025 07:38:15
Module start 'content'
Timing: Jan 31 2025 07:38:15
Module end 'content'
Timing: Jan 31 2025 07:38:15
Script end

Main resources:

Total runtime0.0164 sec
Peak memory usage2,048.0000 KB
Database Queries3

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0059 588.2656151.2266
Module start 'layout' 0.00590.0027 739.492236.6953
Module start 'content' 0.00860.0061 776.187598.3125
Module end 'content' 0.01470.0017 874.500041.9922
Script end 0.0164  916.4922 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.002716.4143140.0002
Check MTime0.00116.5773140.0001
Mysql Total
Database connection0.00074.118410.0007
Mysqli_queries0.002917.790930.0010
Looping result0.00000.074010.0000
Template Total0.00137.610.0013
Template load0.00105.823010.0010
Template processing0.00031.782910.0003
Override
Cache load0.00074.070610.0007
General
dbfile0.002011.952080.0002
String conversion0.00000.049340.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 1
 Number of unique templates used: 1

Time used to render debug report: 0.0002 secs