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.