Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Valid File Names

Status
Not open for further replies.

ralphiooo

Programmer
Oct 23, 2005
64
GB
Hi, i'm trying to do an image upload script. The problem i'm having is the file name part. Originally I created a random number but I realized it was a good idea to have the tile of the image within the name so I've decided to go for the title - some random numbers (to avoid duplicates). I was wondering if anyone knew what things I would have to strip/replace from a title to create a valid file name and what's the best way of doing it. Would appreciate any help. Thanks
 
the valid characters within a file name differ between unix and windows. do a google search to get the full charlists.

a code snip (i haven't run this code so sorry if there are parse errors within):

Code:
$bad_chars = array (";", ":", "/", "\\"); //for example
$filename = "" ; /some file name to test
$replacement_char = "_";
$safefilename = str_replace  
            ($bad_chars, 
             $replacement_char, 
             $filename, 
             $count);

echo "the new filename is $safefilename.  $count bad characters were replaced";
//note you may want to enquote the safefile name if it has spaces in it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top