CodeCharge Studio
search Register Login  

Visual PHP Web Development

Visually Create Internationalized Web Applications, Web Reports, Calendars, and more.
CodeCharge.com

YesSoftware Forums -> CodeCharge Studio -> PHP

 Validating filenames before upload

Print topic Send  topic

Author Message
redExpertos

Posts: 4
Posted: 08/20/2007, 12:47 PM

Hello, I am having problems with a file upload form because my users do speak spanish and usually filenames may have caracters as ñ, acute vowels and spaces.

How do I check if the filename is valid?
What is the best way to link to the file once the file has been loaded to the server?

Thanks for your help.
_________________
Desarrollo en Español
Hosting en Colombia
View profile  Send private message
klwillis


Posts: 428
Posted: 08/21/2007, 1:25 PM

Typically, you'll want to avoid filenames with ñ,acute vowels and spaces.
You could perform some sort of regular expression matching on the
filename(s) used, but someone else here could assist you better with
how to do that most effectively.

You can use a standard link control to refer to the document - provided it's
assessable within your webserver's document directory.

What kinds of files are you uploading? Documents (i.e. PDF)? Images?

Quote redExpertos:
Hello, I am having problems with a file upload form because my users do speak spanish and usually filenames may have caracters as ñ, acute vowels and spaces.

How do I check if the filename is valid?
What is the best way to link to the file once the file has been loaded to the server?

Thanks for your help.

_________________
Kevin Willis, VP/CIO
HealthCare Information Technology Specialist
http://www.nexushealthcare.com
"Fast - Convenient - Quality-Care"

Medical Software Consulting Services
Email : klwillis@nexushealthcare.com
Skype : klwillis2006
View profile  Send private message
klwillis


Posts: 428
Posted: 08/21/2007, 1:36 PM

Here's an example of a client-side check (JavaScript) that
may be helpful in parsing filenames - it's one approach to
hopefully get you started in the right direction:

var fun = document.forms[0].fileUploadName.value;

if (fun.length > 0)
{
// the regex for an image
Regex imageFilenameRegex = new Regex(@"\.(jpg|jpeg|png|gif|pdf)$");
if (imageFilenameRegex.IsMatch(fun))
{
//filename is valid ... procede with upload.
}
}


Quote redExpertos:
Hello, I am having problems with a file upload form because my users do speak spanish and usually filenames may have caracters as ñ, acute vowels and spaces.

How do I check if the filename is valid?
What is the best way to link to the file once the file has been loaded to the server?

Thanks for your help.

_________________
Kevin Willis, VP/CIO
HealthCare Information Technology Specialist
http://www.nexushealthcare.com
"Fast - Convenient - Quality-Care"

Medical Software Consulting Services
Email : klwillis@nexushealthcare.com
Skype : klwillis2006
View profile  Send private message
Hidran
Posted: 08/25/2007, 3:10 AM

I think you should do clean the name in after process file
before is copied to your target folder.
I always do it that way with a regexp:
$name=eregi_replace("([^a-z0-9._-])",'',$filename);
The user can upload a file with what ever name but
I always rename it after my allowed characters.
Hidran
"redExpertos" <redExpertos@forum.codecharge> ha scritto nel messaggio
news:546c9efe1d8f52@news.codecharge.com...
> Hello, I am having problems with a file upload form because my users do
> speak
> spanish and usually filenames may have caracters as ñ, acute vowels and
> spaces.
>
> How do I check if the filename is valid?
> What is the best way to link to the file once the file has been loaded to
> the
> server?
>
> Thanks for your help.
> _________________
> Desarrollo en Español
> Hosting en Colombia
> ---------------------------------------
> Sent from YesSoftware forum
> http://forums.yessoftware.com/
>

redExpertos

Posts: 4
Posted: 08/25/2007, 10:39 AM

Thanks for your recommendation, at least that would solve the problem about the file name, but what is the best way to link to that file once it is loaded into the database?

Cosidering the database field has the original filename and the user will need to download it from the server, if I do change the filename it wouldn't find the file .

On the other hand if I do validate using Javascript it would fail if the browser has it disabled, and that would be a 5% of the internet users.

Any ideas?.
_________________
Desarrollo en Español
Hosting en Colombia
View profile  Send private message
kirchaj

Posts: 215
Posted: 09/17/2007, 11:18 AM

RedExpertos,

I am having the same problem. If you find a solution will you please share it?

Thanks.
TK
View profile  Send private message
redExpertos

Posts: 4
Posted: 09/17/2007, 11:58 AM

kirchaj, what I finally did was check the filename after upload and if it contains not valid characters then I throw an error message, you can mix this with javascript to avoid sending the file to validate it on the server but always check on the server for security.

$filename = $Container->archivo->GetValue();

$name=eregi_replace("([^a-z0-9._-])",'',$filename);

if ($name != $filename) $Component->Errors->addError("El nombre del documento contiene
caracteres no válidos. <br />Por favor use solo caracteres alfanuméricos sin ñ o tildes");

_________________
Desarrollo en Español
Hosting en Colombia
View profile  Send private message
kirchaj

Posts: 215
Posted: 09/17/2007, 1:40 PM

redExpertos,

That is exactly what I was looking for but I really want to catch it before the file upload and stop the upload from happening, or find a way to change the filename and upload name automatically.

Any ideas there?

Thanks for the great reply.

TK
View profile  Send private message
kirchaj

Posts: 215
Posted: 09/26/2007, 11:45 AM

Here is what support suggested as a solution


The desired functionality can be implemented on the client-side using javascript code. In form's client-side OnSubmit event you should check whether the File Upload field value contains forbidden characters and if so through an alert message and return false.

I unfortunately do not know javascript. Anyone out there know how to do this?

Thanks
TK
View profile  Send private message
datadoit.com
Posted: 09/26/2007, 2:12 PM

You only need to know Google. :)

http://www.google.com/search?hl=en&q=javascript+check+f...G=Google+Search
wkempees


Posts: 1679
Posted: 09/26/2007, 3:21 PM

@datadoit & Kirchaj
Have I overseen something?

In the FileUpload I can:
properties->Allowed sum up the extensions I want to be allowed to upload.
properties->Disallowed sum up the extensions I do not want to be uploaded
AND
in the events->OnValidate enter a regular expression and a Error message.
The last in particular will display myError message when the selected file does not match the regexp and will not upload the file, although the interaction suggest otherwise.

Why then the need for javascript?
Am I missing something here?

Walter
_________________
Origin: NL, T:GMT+1 (Forumtime +9)
CCS3/4.01.006 PhP, MySQL .Net/InMotion(Vista/XP, XAMPP)

if you liked this info PAYPAL me: http://donate.consultair.eu
View profile  Send private message
kirchaj

Posts: 215
Posted: 09/27/2007, 8:37 AM

wkempees,

Once again you find the answer. Below is the code I put in the onvalidate event and all seems ok for now. I will do more testing to make sure.

//Get filename to be uploaded into system
$uploadfilename=$NewRecord1->FileUpload1->GetValue();
// check filename for invalid characters
$name=eregi_replace("([^a-z0-9._-]'')",'',$uploadfilename);
if ($name != $uploadfilename)
$Component->Errors->addError("Your filename contains at least one of the following <br> #, &, ?, %, or other invalid web filename characters. <br><br> Please rename the file and try the upload process again.");

Thanks for everyones help.

TK
View profile  Send private message
wkempees


Posts: 1679
Posted: 09/28/2007, 5:20 AM

If you just want to display to user the invalid filenamemessage.
Check out the OnValidate Actions, there already is an action that will validate input throug !preg_match.
It nicely asks you in the properties of the action for the inputmask, and carries an errormessage input as well.

Walter
For the less experienced:
click FileUpload component Properties->Events, OnValidate, AddAction.
List of possible action appears, choose the right one
_________________
Origin: NL, T:GMT+1 (Forumtime +9)
CCS3/4.01.006 PhP, MySQL .Net/InMotion(Vista/XP, XAMPP)

if you liked this info PAYPAL me: http://donate.consultair.eu
View profile  Send private message
kirchaj

Posts: 215
Posted: 10/01/2007, 10:59 AM

Walter,

Sorry to keep beating this one to death, but I cannot find the onvalidate function you are talking about.

Once again, can you point me in the right direction? :-)


Thanks
TK
View profile  Send private message
wkempees


Posts: 1679
Posted: 10/02/2007, 1:48 AM

Picture says more that 1Kwords:


The validation generates a !preg_match

Walter
_________________
Origin: NL, T:GMT+1 (Forumtime +9)
CCS3/4.01.006 PhP, MySQL .Net/InMotion(Vista/XP, XAMPP)

if you liked this info PAYPAL me: http://donate.consultair.eu
View profile  Send private message
kirchaj

Posts: 215
Posted: 10/03/2007, 11:36 AM

Walter,

That is worth 10000 words. Can't wait to see more.

I have tried to implement this with the following expression

(^a-zA-Z0-9._-)

and other variations and it still allows everything through.

What am I missing?

TK
View profile  Send private message

Add new topic Subscribe to topic   


These are Community Forums for users to exchange information.
If you would like to obtain technical product help please visit http://support.yessoftware.com.

MS Access to Web

Convert MS Access to Web.
Join thousands of Web developers who build Web applications with minimal coding.

CodeCharge.com

Home   |    Search   |    Members   |    Register   |    Login


Powered by UltraApps Forum created with CodeCharge Studio
Copyright © 2003-2004 by UltraApps.com  and YesSoftware, Inc.