checking characters in file name for uploads

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

By Deborah - September 17, 2009

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

By gkornbluth - September 17, 2009

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: [Deborah] checking characters in file name for uploads

By gkornbluth - September 17, 2009

Deborah,

You're probably not missing anything. It's unfortunate that there's no "disallow characters" filter in the upload criteria like there is in the text field criteria. (Or maybe I'm missing something).

As hard as we try, getting clients to follow simple rules is sometime just out of reach...

Chris, Dave or another staff member usually checks the forum and might have an idea.

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

By Deborah - September 19, 2009

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