checking characters in file name for uploads

6 posts by 3 authors in: Forums > CMS Builder
Last Post: September 19, 2009   (RSS)

I have a client who uploaded photos with the ampersand character in the file name. This causes the web page the photos are posted on to not validate.

CMS Builder already replaces spaces in file names with dashes or underscores. It would be nice to replace unacceptable characters with another character during upload... (maybe a request for future software releases?)

Deborah

Re: [Deborah] checking characters in file name for uploads

Hi Deborah,

You can do this type of thing yourself using regular expressions. using a "preg_replace" line like the one below to replace the & with a dash.

Sorry this code was for a field name not an image upload but the approach is the same.


<?php foreach ($your_tableRecords as $record): ?>

<?PHP $record['your_field'] = preg_replace("/[&]/", "-", $record['your_field'] ); ?>

<?php echo $record['your_field] ?>

<?php endforeach; ?>


Hope that gets you off in the right direction.

Here's a link to a regex cheat sheet that Dave passed on to me http://www.bitcetera.com/en/techblog/2008/04/01/regex-in-a-nutshell/ and there are a few posts regarding the use of regular expressions on the forum as well.

Best,

Jerry
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Re: [gkornbluth] checking characters in file name for uploads

Jerry,

Thanks very much for the code snippet and link! I remember seeing something about this in other posts, but never got the full explanation.

Although this method would work for replacing characters in a user input field, I'm not sure that preg_replace would be the answer in the situatiion of image uploads. Using preg_replace in the CMS Builder viewer code may replace the ampersand with a dash in code on the web page, but the url to that image would then no longer match the actual image path that contains the ampersand. The filename of the image would need to be rewritten at the time it is uploaded to the server.

Or am I missing something? (Entirely possible... fairly new at PHP.)

Deborah

Re: [Deborah] checking characters in file name for uploads

By Chris - September 19, 2009

Hi Deborah,

You probably just need to add a call to htmlspecialchars().

For example,

INPUT: <img src="<?php echo $upload['thumbUrlPath']; ?>" />
OUTPUT: <img src="/cmsAdmin/uploads/thumb/ampersand&test.png" />


INPUT: <img src="<?php echo htmlspecialchars($upload['thumbUrlPath']); ?>" />
OUTPUT: <img src="/cmsAdmin/uploads/thumb/ampersand&amp;test.png" />


Then again, it might be simpler if CMS Builder automatically converted any potentially tricky characters like you originally suggested. I'll add it to my wishlist. :)
All the best,
Chris

Re: [chris] checking characters in file name for uploads

Chris,

The solution you provided will work for my needs right now. Thanks!

I agree an automatic solution would be best, ultimately.

Thanks again.
Deborah