Share » Forums » Developer » How to add a file to my contentobject...

How to add a file to my contentobject through PHP?

How to add a file to my contentobject through PHP?

Saturday 01 October 2005 3:49:50 am - 6 replies

Modified on Saturday 01 October 2005 3:50:34 am by Clemens T

Author Message

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

You must be logged in to post messages in this topic!

36 542 Users on board!

Forums menu