Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 03-13-2003, 06:56 PM   #1 (permalink)
Admin
$_['Your_Mom'];
 
Admin's Avatar
 
Join Date: May 2002
Location: Santee
Posts: 627
Admin is on a distinguished road
safe file naming?

is there a function to create safe file names? i cant seem to locate any

something like this:

PHP Code:
$foo "this !!! isn't a good";
$bar " ## file name";

$filename someFunction("$foo-$bar");

echo 
"$filename"
and $filename would return something like this: "this_isnt_a_good_file_name"

anything?
__________________


Urban Clothing
Admin is offline   Reply With Quote
Old 03-13-2003, 07:02 PM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,490
sde is on a distinguished road
what about a bunch of str_replace() things
PHP Code:
<?
function goodName($filename)
{
  
$filename=str_replace(" ","_",$filename);

  
// caution, i don't know regex that well so this is probably wrong.
  // but use a match that matches only things that are NOT
  // alphanumeric chars & underscore
  // and replace them with "" nothing.
  
$filename=preg_replace("[^A-Za-z0-9_]","",$filename);

  return 
$filename;
}
?>
sde is offline   Reply With Quote
Old 03-13-2003, 07:11 PM   #3 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
what's wrong with that filename? works on windows and linux.

most unixlike systems will allow everything in a filename except tab, linefeed, carriage return, and forward slash. (did i miss any)?

windows does not allow the ones above plus the following:
\ / " * ? < > |

do an strtr() on characters you don't like.
joe_bruin is offline   Reply With Quote
Old 03-13-2003, 07:27 PM   #4 (permalink)
Admin
$_['Your_Mom'];
 
Admin's Avatar
 
Join Date: May 2002
Location: Santee
Posts: 627
Admin is on a distinguished road
browsers dont seem to like # in the filename. i figure there are more characters that arent accepted.

ill check that function out, thanks.
__________________


Urban Clothing
Admin is offline   Reply With Quote
Old 03-13-2003, 09:02 PM   #5 (permalink)
abc123
bloomberg
 
abc123's Avatar
 
Join Date: Jun 2002
Location: bloomberg
Posts: 263
abc123 is on a distinguished road
Send a message via AIM to abc123 Send a message via Yahoo to abc123
browsers don't like literal "#" and special characters in url's but you you can encode as odd ones you can think of, i.e: "%20" represents " " (space).


just a note, you cannot name any file in windows beginning with "con".

i.e:
con.exe
con.txt
con.bat
con.java
con.cpp


none of these will work, windows doesn't allow it!
__________________
-- bloomberg.
abc123 is offline   Reply With Quote
Old 03-14-2003, 06:22 AM   #6 (permalink)
Ilya020
Techno Rat
 
Ilya020's Avatar
 
Join Date: Jan 2003
Location: San Diego
Posts: 559
Ilya020 is on a distinguished road
Send a message via AIM to Ilya020
Quote:
Originally posted by abc123
browsers don't like literal "#" and special characters in url's but you you can encode as odd ones you can think of, i.e: "%20" represents " " (space).


just a note, you cannot name any file in windows beginning with "con".

i.e:
con.exe
con.txt
con.bat
con.java
con.cpp


none of these will work, windows doesn't allow it!
why not??

Ilya.
__________________
> SELECT * FROM users WHERE clue > 0
0 rows returned
Ilya020 is offline   Reply With Quote
Old 03-14-2003, 02:21 PM   #7 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
"con" is the console filename. it is reserved.

try doing "copy somefile.txt con" in a command window, and you'll see that it writes it to the console.
joe_bruin is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 06:40 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting