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.

eZ debug

Timing: Jan 18 2025 11:33:36
Script start
Timing: Jan 18 2025 11:33:36
Module start 'layout'
Timing: Jan 18 2025 11:33:36
Module start 'content'
Timing: Jan 18 2025 11:33:37
Module end 'content'
Timing: Jan 18 2025 11:33:37
Script end

Main resources:

Total runtime1.1286 sec
Peak memory usage4,096.0000 KB
Database Queries63

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0061 587.9141152.6250
Module start 'layout' 0.00620.0034 740.539139.4453
Module start 'content' 0.00961.1173 779.9844585.3672
Module end 'content' 1.12690.0017 1,365.351616.1563
Script end 1.1285  1,381.5078 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00350.3139160.0002
Check MTime0.00140.1253160.0001
Mysql Total
Database connection0.00070.060910.0007
Mysqli_queries1.048992.9406630.0166
Looping result0.00060.0545610.0000
Template Total1.089696.520.5448
Template load0.00210.183620.0010
Template processing1.087696.364820.5438
Template load and register function0.00020.017210.0002
states
state_id_array0.00090.082910.0009
state_identifier_array0.00220.192320.0011
Override
Cache load0.00200.17351220.0000
Sytem overhead
Fetch class attribute can translate value0.00080.069720.0004
Fetch class attribute name0.00100.085960.0002
XML
Image XML parsing0.00060.053420.0003
class_abstraction
Instantiating content class attribute0.00000.001570.0000
General
dbfile0.00090.0785160.0001
String conversion0.00000.001040.0000
Note: percentages do not add up to 100% because some accumulators overlap

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

Time used to render debug report: 0.0001 secs