valid filenaming

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
puke7
Forum Newbie
Posts: 12
Joined: Sat May 10, 2008 11:49 am

valid filenaming

Post by puke7 »

I've got a website where people have uploaded mp3s over the past couple years. I'm looking to revamp a whole lot of the site because I basically learned php while developing it and it's loaded with every poor design decision you can imagine. I'm looking for critiques on my method before I implement it.

My filenaming convention is "TrackID ArtistName - TrackTitle.mp3" renamed from whatever the uploaded files' original names were using user info and the TrackTitle entered in the upload form. Up until now my HTML charset has been undeclared and I am now switching it to utf8. So I will be using

Code: Select all

$filename = utf8_accents_to_ascii($filename);    
$filename = utf8_strip_non_ascii($filename);
from the phputf8 library.

I alos know DOS/Windows will reject the following characters \ / : * ? " < > |
From what I can tell Apache will let you do whatever you want. If you have a backslash (/) in the filename it will just create necessary folders to make it work and the file is still accessible in that way.
I don't know anything about the Mac platform.

To negate the invalid ascii characters I came up with this --

Code: Select all

function DOSfriendly($filename)  {
  $filename = str_replace(array("\\","/",":","*","?",'"',"<",">","|"),
                          array( "µ","¶","~","¤","¿","¨","(",")","I"),$filename);
  return $filename;
}
However, now that I am thinking about it, could users enter ascii command characters in the Track Title field? Is that something to worry about?

Any feedback on this concept would be appreciated!
User avatar
puke7
Forum Newbie
Posts: 12
Joined: Sat May 10, 2008 11:49 am

Re: valid filenaming

Post by puke7 »

...ah so my approach remains flawless! =D
Glycerine
Forum Commoner
Posts: 39
Joined: Wed May 07, 2008 2:11 pm

Re: valid filenaming

Post by Glycerine »

HA indeed. I see you are indeed flawless. Yet - When I came across a similar problem - I tried to do the same. Yet Regex'ing yields much studier results. This allows for all those miscellaneous (YOU KNOW HOW LONG IT TOOK ME TO SPELL THAT) characters we meer mortals forget.

Unfortunately I cannot grace you with the answer. I no little about pregmatic matching. I'm guessing it'll have [a-z][A-Z][0-9] in it. which mean all the letters on your keyboard as you can probably tell. I'd certainly look into it, because say for example someone enters Japanese... your buggered!
User avatar
puke7
Forum Newbie
Posts: 12
Joined: Sat May 10, 2008 11:49 am

Re: valid filenaming

Post by puke7 »

oh noes -- I still live in fear of regex!! =X
Glycerine
Forum Commoner
Posts: 39
Joined: Wed May 07, 2008 2:11 pm

Re: valid filenaming

Post by Glycerine »

ditto but its a need...
Post Reply