Gossamer Forum
Home : Products : DBMan : Customization :

remove spaces from uploaded files how? ( file upload mod )

Quote Reply
remove spaces from uploaded files how? ( file upload mod )
I have the file upload mod working (multiple file upload mod version installed for future, but for now just using it for one upload per record...)

I also am trying to integrate imagemagick to do thumbnails, which works except one thing: when somone uploads a file with a space in the name, imagemagick can't find the file.

I've used the following code which I got from the forum here to strip commas and dollar signs from prices so that they are all on level playing field:

$in{'price'} =~ s/,//g; ### remove comma from entry
$in{'price'} =~ s/\$//g; ### remove dollar sign from entry

However I don't seem to have any luck applying something like:

$in{'file-to-upload-1'} =~ s/ //g; ### remove space from filename

or

$in{'filename'} =~ s/ //g; ### remove space from filename

Doens't do anything and the upload file saved on the local test server still has the spaces in it and the browser shows it as image%20name and imagemagick can't find it to do the thumbnail.

I tried adding those lines to the top of sub validate_upload { and then with no luck above the line:

$filename = $1;

and then with no luck above

push(@Files_Written, "$SAVE_DIRECTORY\/$newdirname\/$filename");

and also in the sub add_record { itself.

Any ideas on how to strip the spaces from filenames people upload using hte multi-file upload mod?

Thanks very much in advance.
Quote Reply
Re: [beaverbeacon] remove spaces from uploaded files how? ( file upload mod ) In reply to
This is taken from the FAQ:

Quote:
For stripping out spaces and currency symbols, you would add some code to sub add_record. The code to strip characters from the fields would go in sub add_record, just before: $status = &validate_record;

To strip out spaces, use code: $in{'fieldname'} =~ s/ //g;

Currency symbols can be tricky. Would it just be dollar signs or would it be other symbols, too?

If it's just dollar signs, use code: $in{'fieldname'} =~ s/\$//g;

It looks like you were right with your initial attempt, try adding it to the sub add_record and see if that helps any.

Good Luck!
Quote Reply
Re: [Watts] remove spaces from uploaded files how? ( file upload mod ) In reply to
Thanks for the reply Watts, but I just tried it again, and it doesn't work for the uploaded filename.

Here are the latest attempts:

Code:
sub add_record {
# --------------------------------------------------------
# Adds a record to the database. First, validate_record is called
# to make sure the record is ok to add. If it is, then the record is
# encoded and added to the database and the user is sent to
# html_add_success, otherwise the user is sent to html_add_failure with
# an error message explaining why. The counter file is also updated to the
# next number.

my ($output, $status, $counter);
# Set the userid to the logged in user.
($auth_user_field >= 0) and ($in{$db_cols[$auth_user_field]} = $db_userid);

# First we validate the record to make sure the addition is ok.
$in{'file-to-upload-1'} =~ s/ //g; ### remove space from entry
$in{'file-to-upload-1'} =~ s/%20//g; ### remove space from entry

if ($in{'file-to-upload-1'}) { $in{'Graphic'} = 'Yes'; }
else { $in{'Graphic'} = ''; }
$in{'price'} =~ s/,//g; ### remove comma from entry
$in{'file-to-upload-1'} =~ s/ //g;
$in{'file-to-upload'} =~ s/ //g;
$in{'filename'} =~ s/ //g;

$in{'price'} =~ s/\$//g; ### remove dollar sign from entry

$status = &validate_record;

<snip>


The lines in blue above do work fine to remove dollar signs and commas from the price field. However the lines in red above seem to have no effect at all. Uploading a file with the name "Copy of Picture for Web.jpg" ends up loading in the browser as "Copy%20of%20Picture%20for%20Web.jpg" and gets saved in the image directory with the spaces in the filename still.

I can't figure out why it doesn't work.

Last edited by:

beaverbeacon: Sep 8, 2003, 1:02 PM
Quote Reply
Re: [beaverbeacon] remove spaces from uploaded files how? ( file upload mod ) In reply to
With the multiple file upload it doesn't store the filename of the graphic .. The mod creates a directory for the files with a name to match the key value of the associated record.

I'm not sure how your process is different using imagemajic but I would advise people not to upload files with spaces in the name.

You should include instructions when adding a record that photos must NOT contain any spaces in their names.

Unoffical DBMan FAQ

http://creativecomputingweb.com/dbman/index.shtml/
Quote Reply
Re: [beaverbeacon] remove spaces from uploaded files how? ( file upload mod ) In reply to
Heres what I did when experimenting with dbman..

In sub validate_upload

Code:
foreach $key (sort {$a <=> $b} $query->param()) {
next if ($key =~ /^\s*$/);
next if ($query->param($key) =~ /^\s*$/);
next if ($key !~ /^filetoupload(\d+)$/);
$Number = $1;
++$num_files;
if ($query->param($key) =~ /([^\/\\]+)$/) {
$filename = $1; # added for replacing spaces in file names with underscores
$filename =~ s/\s+/_/g;
$filename =~ s/\%+/_/g;
# end addition



$File_Handle = $query->param($key);
unless ($filename =~ /$ALLOWED_EXT/) {
$ALLOWED_EXT =~ s/\\//g;
$ALLOWED_EXT =~ s/\$//g;
@ext = split (/\Q|\E/o,$ALLOWED_EXT);
$ALLOWED_EXT = join(" or ",@ext);
return "Only files with the following extension(s) are allowed: $ALLOWED_EXT";
Display above is weird, this was the part for getting rid of spaces and any other urlencoding and replacing with underscores..

Code:
# added for replacing spaces in file names with underscores
$filename =~ s/\s+/_/g;
$filename =~ s/\%+/_/g;
# end addition




chmod
Quote Reply
Re: [chmod] remove spaces from uploaded files how? ( file upload mod ) In reply to
Fantastic!!! Thank you! Thank you! Thank you! The above works perfectly. I really appreciate your help.
Quote Reply
Re: [beaverbeacon] remove spaces from uploaded files how? ( file upload mod ) In reply to
Thanks (too) CHMOD... I kept looking at this:

=~ s/ //g;

and I couldn't help but think that Perl would not simply allow a space " " to be physically typed into a regular expression, but would instead use some code (s+) to represent a space. I looked around on PerlDoc.com, but I find that site to be hard to navigate and didn't have any luck. I've misplaced my 'Perl for Dummies' book (really) and haven't had a chance to replace it yet.

Does anyone have a favorite Perl reference site for looking up simple regex/syntax questions?