Forums / Developer / How to add a file to my contentobject through PHP?

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

eZ debug

Timing: Jan 19 2025 00:27:12
Script start
Timing: Jan 19 2025 00:27:12
Module start 'content'
Timing: Jan 19 2025 00:27:13
Module end 'content'
Timing: Jan 19 2025 00:27:13
Script end

Main resources:

Total runtime0.7652 sec
Peak memory usage4,096.0000 KB
Database Queries206

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0075 588.9766180.8125
Module start 'content' 0.00750.6287 769.7891595.7188
Module end 'content' 0.63620.1289 1,365.5078341.0547
Script end 0.7651  1,706.5625 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00440.5720210.0002
Check MTime0.00160.2079210.0001
Mysql Total
Database connection0.00080.098810.0008
Mysqli_queries0.683889.35832060.0033
Looping result0.00200.26672040.0000
Template Total0.734796.020.3674
Template load0.00240.310820.0012
Template processing0.732495.708620.3662
Template load and register function0.00030.032810.0003
states
state_id_array0.00090.118310.0009
state_identifier_array0.00070.096220.0004
Override
Cache load0.00210.2719350.0001
Sytem overhead
Fetch class attribute can translate value0.00150.197130.0005
Fetch class attribute name0.00130.170280.0002
XML
Image XML parsing0.00110.146130.0004
class_abstraction
Instantiating content class attribute0.00000.0036100.0000
General
dbfile0.00310.4021270.0001
String conversion0.00000.001230.0000
Note: percentages do not add up to 100% because some accumulators overlap

CSS/JS files loaded with "ezjscPacker" during request:

CacheTypePacklevelSourceFiles
CSS0extension/community/design/community/stylesheets/ext/jquery.autocomplete.css
extension/community_design/design/suncana/stylesheets/scrollbars.css
extension/community_design/design/suncana/stylesheets/tabs.css
extension/community_design/design/suncana/stylesheets/roadmap.css
extension/community_design/design/suncana/stylesheets/content.css
extension/community_design/design/suncana/stylesheets/star-rating.css
extension/community_design/design/suncana/stylesheets/syntax_and_custom_tags.css
extension/community_design/design/suncana/stylesheets/buttons.css
extension/community_design/design/suncana/stylesheets/tweetbox.css
extension/community_design/design/suncana/stylesheets/jquery.fancybox-1.3.4.css
extension/bcsmoothgallery/design/standard/stylesheets/magnific-popup.css
extension/sevenx/design/simple/stylesheets/star_rating.css
extension/sevenx/design/simple/stylesheets/libs/fontawesome/css/all.min.css
extension/sevenx/design/simple/stylesheets/main.v02.css
extension/sevenx/design/simple/stylesheets/main.v02.res.css
JS0extension/ezjscore/design/standard/lib/yui/3.17.2/build/yui/yui-min.js
extension/ezjscore/design/standard/javascript/jquery-3.7.0.min.js
extension/community_design/design/suncana/javascript/jquery.ui.core.min.js
extension/community_design/design/suncana/javascript/jquery.ui.widget.min.js
extension/community_design/design/suncana/javascript/jquery.easing.1.3.js
extension/community_design/design/suncana/javascript/jquery.ui.tabs.js
extension/community_design/design/suncana/javascript/jquery.hoverIntent.min.js
extension/community_design/design/suncana/javascript/jquery.popmenu.js
extension/community_design/design/suncana/javascript/jScrollPane.js
extension/community_design/design/suncana/javascript/jquery.mousewheel.js
extension/community_design/design/suncana/javascript/jquery.cycle.all.js
extension/sevenx/design/simple/javascript/jquery.scrollTo.js
extension/community_design/design/suncana/javascript/jquery.cookie.js
extension/community_design/design/suncana/javascript/ezstarrating_jquery.js
extension/community_design/design/suncana/javascript/jquery.initboxes.js
extension/community_design/design/suncana/javascript/app.js
extension/community_design/design/suncana/javascript/twitterwidget.js
extension/community_design/design/suncana/javascript/community.js
extension/community_design/design/suncana/javascript/roadmap.js
extension/community_design/design/suncana/javascript/ez.js
extension/community_design/design/suncana/javascript/ezshareevents.js
extension/sevenx/design/simple/javascript/main.js

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
7content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
12content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
2content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
5content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
3content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 31
 Number of unique templates used: 7

Time used to render debug report: 0.0002 secs