Gossamer Forum
Home : Products : DBMan : Customization :

Problems with users and file upload (with solution to spaces in file name problem)

Quote Reply
Problems with users and file upload (with solution to spaces in file name problem)
Hi,

This is more of a problem with my users than with the Multi-files upload hack, but...

Is there any way to simply substituted Spaces with - signs so images uploaded will be displayed properly. People obviously can't read, and it would probably be more trouble than it is worth to try to write the code to replace the spaces for them...

So far this morning 30 people have uploaded files with spaces in the file names, even though I have given them specific instructions not to do so,No file names that have spaces in them, just below the upload area along with a link to instructions on how to upload images in the options area. Actually I have problem with non-english file name aswell,is it possible to substituted non-english charecter file name with english charecter so images uploaded will be displayed properly.Dont forget that I have multi-files upload so the non-english charecter must not the same for each uploaded file,so it should be randomed charecter to avoid repeat same name.....

Once I ever follow

Any thoughts?
Love you all,and thank for all help here ,this is a greatest forum.
Act.
Quote Reply
Re: [act] Problems with users and file upload (with solution to spaces in file name problem) In reply to
First, on the space issue:
To disable spaces, you could do this:

if ($in{'filename'} =~ /\s+/) { &html_add_failure("Please note that filenames must not contain spaces");}

If your file upload routine uses sub validate_upload, then you could add this line there, but you might have to change $in{'filename'} to whatever variable is used for the filename input:

if ($filename =~ /\s+/) { return("Please note that filenames must not contain spaces");}

As for the non-English character check, the only solution I can think of is to build a list of allowed characters and then check whether the filename *only* contains them. Perhaps someone else has a better idea?
kellner
Quote Reply
Re: [kellner] Problems with users and file upload (with solution to spaces in file name problem) In reply to
I had to do something similar anyway, so here's some code for you that checks on occurrence of "illegal characters", including spaces as well as non-English letters.

You can use it like this:
my $filenamestatus = &check_filename("$filename");
if ($filenamestatus eq "ok") { ... do stuff to upload file;}
else { &cgierr("$filenamestatus");}

This is the subroutine that does the checking:
Code:
sub check_filename {
my $checkstring = $_[0];
my (%allowed_hash, %errors, $returnstring);
if ($checkstring =~ /\s+/g) {# one or more instances of whitespace?
$returnstring = "Error! The filename must not contain spaces.\n";
$checkstring =~ s/\s+//g;
}

my @allowed_characters = qw(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z _ . +); # modify this list as you like;
# include punctuation marks etc. that you want to allow in filenames

foreach (@allowed_characters) { $allowed_hash{$_} = 1;}

my @stringarray = split //, $checkstring;
foreach my $element (@stringarray) {
unless ($allowed_hash{$element}) { $errors{$element} = 1;};
}

if (%errors) {
$returnstring .= qq|Error! The filename contains the following illegal characters:<font color="red">|;
foreach (keys %errors) { $returnstring .= "$_ ";}
$returnstring .= qq|<\font>|;
}
else { $returnstring = "ok";}

return ($returnstring);
}
I'm sure this could be made much simpler :-)

kellner
Quote Reply
Re: [kellner] Problems with users and file upload (with solution to spaces in file name problem) In reply to
Thank for you reply before try your code , I want to say ,I want to allow user upload files,but if the name of file contain with space,that space would be replaced
with "-"
Hope you can help me .

I will try the code about non-english later,thank you very much.
Love you all,and thank for all help here ,this is a greatest forum.
Act.
Quote Reply
Re: [act] Problems with users and file upload (with solution to spaces in file name problem) In reply to
ok, then just omit that part of the above code that takes care of the spaces and instead add this line in add_record in db.cgi:

if ($in{'filename'}) { #if this is a file upload
$in{'filename'} =~ s/\s/-/g; #replace all spaces with "-"
}

kellner
Quote Reply
Re: [kellner] Problems with users and file upload (with solution to spaces in file name problem) In reply to
You may want \s+ because if someone enters:
Code:
This is a file name

you'll get....

This----is-a-file--name

Last edited by:

PaulW: Nov 19, 2001, 8:21 AM
Quote Reply
Re: [kellner] Problems with users and file upload (with solution to spaces in file name problem) In reply to
I have tried your code and try this code
$filename =~ s/\s/-/g; #replace all spaces with "-"

add before

if (!open(OUTFILE, ">$SAVE_DIRECTORY\/$newdirname\/$filename")) {

but not success.

Any help please.
Love you all,and thank for all help here ,this is a greatest forum.
Act.
Quote Reply
Re: [act] Problems with users and file upload (with solution to spaces in file name problem) In reply to
In sub validate_upload, you have this code:
Code:
if ($query->param($key) =~ /([^\/\\]+)$/) {
$filename = $1;

Ad this line right afterwards:

$filename =~ s/\s+/-/g;


kellner
Quote Reply
Re: [kellner] Problems with users and file upload (with solution to spaces in file name problem) In reply to
yea!!:)) working great!!
I got and idea to deal with non-english charecter,if file name with non-english and special charecter,(a-z A-Z 0-9), uploading....then
replace each non-english charecter with english charecter(a-z A-Z 0-9)
But how to replace which non-english char.with which english char??


This modify almost finished!!!!! GOOD JOB. ;)
Love you all,and thank for all help here ,this is a greatest forum.
Act.


Last edited by:

act: Nov 20, 2001, 6:49 AM
Quote Reply
Re: [kellner] Problems with users and file upload (with solution to spaces in file name problem) In reply to
I also got the problem with display an uploaded image on the sub html_record_long.
I got a modify to place an image on the long html and others upload just link to.
The image will broken not show properly if user has removed the first image and not make a new upload.

Code:
# put the photos into an array called @photo
if (-e "$SAVE_DIRECTORY/$rec{$db_key}") {
opendir (GRAPHIC, "$SAVE_DIRECTORY/$rec{$db_key}") or &cgierr("unable to open directory: $SAVE_DIRECTORY/$rec{$db_key}. Reason: $!");
@files = readdir(GRAPHIC);
closedir (GRAPHIC);
@photo = qw();
foreach $file (@files) {
next if ($file =~ /^\./); # Skip "." and ".." entries..
next if ($file =~ /^index/); # Skip index.htm type files..
push(@photo, $file);
}
}

# then to print out individual photos on the page
print qq|<div align="center"><table border="0" $displaybg2 cellspacing="0" cellpadding="8">
<tr><td><img src="$SAVE_DIRECTORY_URL/$rec{$db_key}/$photo[0]"></td></tr></table></div>|;

Q:How can it show another image instead and if user dont want to show that another image,then let he choose whhich image he want to be displayed. may be just ask he to upload a file name "1.gif or 1.jpg" and if 1.gif/jpg available then display 1.gif/jpg ,else display another.

Any comment?
Love you all,and thank for all help here ,this is a greatest forum.
Act.

Last edited by:

act: Nov 20, 2001, 6:50 AM
Quote Reply
Re: [act] Problems with users and file upload (with solution to spaces in file name problem) In reply to
Well, replacing each non-English character with an English character doesn't sound a real good idea to me - you'd have to specify, for *every single* non-English character (and there's LOTS of them!) - what you'd like to replace it with. A lot of effort, and you'll end up with a lot of real strange filenames.

I'd prefer the other approach: Define a list of acceptable characters (a-z, A-Z, 0-9, plus ".", "_", and perhaps a few others), and return an error message to the user asking to submit a corrected filename when they enter non-English characters.
kellner
Quote Reply
Re: [kellner] Problems with users and file upload (with solution to spaces in file name problem) In reply to
Where to put the code in?
Love you all,and thank for all help here ,this is a greatest forum.
Act.
Quote Reply
Re: [kellner] Problems with users and file upload (with solution to spaces in file name problem) In reply to
I have new idea,if uploaded file has a single non-english charecter then ,change the name to 1.jpg/gif (number file name) by 1 would not been used again for another non-english file name.I am not a programming literate sorry if my idea not work but I think it is possible to use counting for the renamed file. Some of my user never even rename his file so I need the uploading rename file to them. or they will not use my db.
Love you all,and thank for all help here ,this is a greatest forum.
Act.
Quote Reply
Re: [kellner] Problems with users and file upload (with solution to spaces in file name problem) In reply to
Sometime user have uploaded more than one file and some are english name but some not.

Love you all,and thank for all help here ,this is a greatest forum.
Act.