Forums / General / Image import - generate file names

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

eZ debug

Timing: Jan 18 2025 05:01:50
Script start
Timing: Jan 18 2025 05:01:50
Module start 'content'
Timing: Jan 18 2025 05:01:51
Module end 'content'
Timing: Jan 18 2025 05:01:51
Script end

Main resources:

Total runtime0.7194 sec
Peak memory usage4,096.0000 KB
Database Queries200

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0098 587.7109180.8359
Module start 'content' 0.00980.5791 768.5469590.4453
Module end 'content' 0.58890.1305 1,358.9922341.3750
Script end 0.7193  1,700.3672 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00420.5847210.0002
Check MTime0.00140.1990210.0001
Mysql Total
Database connection0.00160.223510.0016
Mysqli_queries0.626987.14162000.0031
Looping result0.00200.27191980.0000
Template Total0.685795.320.3428
Template load0.00210.287620.0010
Template processing0.683695.024620.3418
Template load and register function0.00020.021910.0002
states
state_id_array0.00080.117510.0008
state_identifier_array0.00080.104720.0004
Override
Cache load0.00200.27211220.0000
Sytem overhead
Fetch class attribute can translate value0.00130.187030.0004
Fetch class attribute name0.00110.155760.0002
XML
Image XML parsing0.00090.126730.0003
class_abstraction
Instantiating content class attribute0.00010.019270.0000
General
dbfile0.00550.7582270.0002
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
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
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 30
 Number of unique templates used: 6

Time used to render debug report: 0.0002 secs