Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
check if enterd name is a valid filename
Old 02-11-2008, 02:26 PM check if enterd name is a valid filename
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
hi,

on my file manager im just doing the rename func, how can i check if the name is valid?
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
 
Register now for full access!
Old 02-11-2008, 02:51 PM Re: check if enterd name is a valid filename
Skilled Talker

Posts: 62
Name: Daniel
Trades: 0
file_exists() ?
castis is offline
Reply With Quote
View Public Profile
 
Old 02-11-2008, 04:05 PM Re: check if enterd name is a valid filename
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
nope i needed it to see if a string would be valid.

i have done it now using regex [a-zA-Z1-9_-]
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 02-11-2008, 05:23 PM Re: check if enterd name is a valid filename
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
This should help:

PHP Code:
$sanitized_name preg_replace('/[^0-9a-z\.\_\-]/i','',$original_name);
if (
$sanitized_name == $original_name) {
  echo 
"Great file name!";
} else {
  echo 
"Filename problems.  Try ".$sanitized_name;

__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 02-12-2008, 11:06 AM Re: check if enterd name is a valid filename
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
that looks better than what im using but could u just explain what the regex is doing not quite sure i get the 0-9 and a-z (shouldnt it also have A-Z for caps?) but whats the /i???
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 02-12-2008, 12:34 PM Re: check if enterd name is a valid filename
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
It replaces everything which is not a 0-9 a-z - _ or . case-insensitively (the i at the end).
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 02-12-2008, 12:59 PM Re: check if enterd name is a valid filename
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
Thanks i get what u mean now
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 02-13-2008, 11:53 AM Re: check if enterd name is a valid filename
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
Hi again,

This i causing errors

imysing this:
PHP Code:
if(empty($newname) && $sanitized_name == $newname)
{
 if(
rename(DOC_ROOT.''.$cur_dir.'/'.$fileDOC_ROOT.''.$cur_dir.$newname))
 {
 echo 
'<script type="text/javascript"> window.location = "'.SITE_URL.'/admin/file_manager.php?dir='.$cur_dir.'" </script>';
 }
 else {
 echo 
'<script type="text/javascript">
      alert("ERROR: the file does not appear to have been successfully renamed! - Contact Admin");
  window.location = "'
.SITE_URL.'/admin/file_manager.php?dir='.$cur_dir.'";
  </script>'
;
  }
}
else {
 echo 
"<script>alert('Filename problems.  Try ".$sanitized_name."');</script>";  

and it seems to be the preg_replace and regex which is causing it, it always return false, and the stanistized name is empty. so i think somethings wrong

it seems it removes all valud chars,

so should i have if($sanitized !== '') so if there was any chars it didnt remove then i know its a bad name?
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 02-13-2008, 06:09 PM Re: check if enterd name is a valid filename
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
Can we see the code where the regex is applied?
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 02-14-2008, 05:32 AM Re: check if enterd name is a valid filename
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
figured it out, it was this:

PHP Code:
 
if(empty($newname) && $sanitized_name == $newname
</SPAN></SPAN>

it need a !before the empty() so it was reading false.

Dan
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 02-14-2008, 12:45 PM Re: check if enterd name is a valid filename
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
Good catch! Glad you got it figured out.
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 02-14-2008, 01:11 PM Re: check if enterd name is a valid filename
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
God i wish someone (hint hint) would fix that bug!! its peeving me off! when u have code it continues the colour and shows </span>'s GRR
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Reply     « Reply to check if enterd name is a valid filename
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.69093 seconds with 12 queries