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
|
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
|