Image import - generate file names

Author Message

Roy Viggo Pedersen

Monday 11 August 2003 1:06:48 pm

Hi!

I've made a script to import images from a directory into eZ publish. It works fine, but uses the original file name.

I want to use the same method for naming files as in eZ, any tips on how to do that?

Roy

Ekkehard Dörre

Tuesday 12 August 2003 12:06:26 am

Hi,

the renaming is because of browserupload in php in the temporary tmp folder. You can make unixsafe filenames with:

$saferImageName = strtolower( $image );
$saferImageName = preg_replace( array( "/[^a-z0-9_\. ]/" ,
"/ /",
"/__+/" ),
array( "",
"_",
"_" ),
$saferImageName );
rename($uploadFtpDir."/".$image,$uploadFtpDir."/".$saferImageName);

$image is original image name
$saferimagename is the new name
$uploadFtpDir is the ftpdir for batchupload

or the total batchuploadclass as a hack in progress with errors:
http://ez.no/developer/ez_publish_3/contributions/image_batch_uploadscript_with_errors

Greetings ekke

http://www.coolscreen.de - Over 40 years of certified eZ Publish know-how: http://www.cjw-network.com
CJW Newsletter: http://projects.ez.no/cjw_newsletter - http://cjw-network.com/en/ez-publ...w-newsletter-multi-channel-marketing

Roy Viggo Pedersen

Tuesday 12 August 2003 11:31:01 am

Thanks ekke! I wasn't aware of that.

Your import script is very similar to mine, but I wanted new, and as it turned out, not quite random, but unique file names.

So I made a function to generate unique file names, but similar on each run:

$newFileName = makeImageFileName($fileName, $contentObject);

function makeImageFileName($fileName="", $node=false)
{
if (!$fileName || !$node) return;
$extension = preg_replace('/.*\.([A-z]{2,4})$/', '\\1', $fileName);
$nodeID = $node->attribute('id');
$nodeClass =& eZContentClass::fetch($node->attribute('contentclass_id'));
$identName = $nodeClass->attribute('identifier');
$newFileName = $identName . $nodeID . '.' . strtolower($extension);
return $newFileName;
}

Roy

Ekkehard Dörre

Wednesday 13 August 2003 12:56:57 am

Hi,

I uploaded it because of Your posting, because there are errorwarnings. Fell free to change it or please upload yours, when it is working.
Part from http://pear.php.net/package-info.php?package=HTTP_Upload
// This software is licensed by the LGPL
// -> http://www.gnu.org/copyleft/lesser.txt
// (c) 2001 by Tomas Von Veschler Cox

/**
* Sets the name of the destination file
*
* @param string $mode A valid mode: 'uniq', 'safe' or 'real' or a file name
* @param string $prepend A string to prepend to the name
* @param string $append A string to append to the name
*
* @return string The modified name of the destination file
* @access public
*/
function setName($mode, $prepend=null, $append=null)
{
switch ($mode) {
case 'uniq':
$name = $this->nameToUniq();
$this->upload['ext'] = $this->nameToSafe($this->upload['ext'], 10);
$name .= '.' . $this->upload['ext'];
break;
case 'safe':
$name = $this->nameToSafe($this->upload['real']);
if (($pos = strrpos($name, '.')) !== false) {
$this->upload['ext'] = substr($name, $pos + 1);
} else {
$this->upload['ext'] = '';
}
break;
case 'real':
$name = $this->upload['real'];
break;
default:
$name = $mode;
}
$this->upload['name'] = $prepend . $name . $append;
$this->mode_name_selected = true;
return $this->upload['name'];
}

/**
* Unique file names in the form: 9022210413b75410c28bef.html
* @see HTTP_Upload_File::setName()
*/
function nameToUniq()
{
if (! $this->_seeded) {
srand((double) microtime() * 1000000);
$this->_seeded = 1;
}
$uniq = uniqid(rand());
return $uniq;
}

/**
* Format a file name to be safe
*
* @param string $file The string file name
* @param int $maxlen Maximun permited string lenght
* @return string Formatted file name
* @see HTTP_Upload_File::setName()
*/
function nameToSafe($name, $maxlen=250)
{
$noalpha = 'áéíóúàèìòùäëïöüÁÉÍÓÚÀÈÌÒÙÄËÏÖÜâêîôûÂÊÎÔÛñçÇ@';
$alpha = 'aeiouaeiouaeiouAEIOUAEIOUAEIOUaeiouAEIOUncCa';
$name = substr ($name, 0, $maxlen);
$name = strtr ($name, $noalpha, $alpha);
// not permitted chars are replaced with "_"
return ereg_replace ('[^a-zA-Z0-9,._\+\()\-]', '_', $name);
}

Greetings, ekke

http://www.coolscreen.de - Over 40 years of certified eZ Publish know-how: http://www.cjw-network.com
CJW Newsletter: http://projects.ez.no/cjw_newsletter - http://cjw-network.com/en/ez-publ...w-newsletter-multi-channel-marketing

Roy Viggo Pedersen

Wednesday 13 August 2003 6:02:56 am

Hi ekke,

Thanks for the functions, nice.

I used the same function saveImage as you did, but added a check that the source file exist. You should also check that the file copy was a success before unlinking :-)

Roy

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.