PHP - renaming files which have an "/" in the name? (Or: gad zooks! Who's the idiot?!

Posted:
in Genius Bar edited January 2014
Ok this is giving me complete fits (I also posted this on another forum, but need an answer fast - don't kill me if you see this question somewhere else!).



I am using PHP in OS X. I know you aren't supposed to name a file with a "/" in it - and most systems won't even let you if they are smart. But in their infinite wisdom at 1 infinite loop, the / is allowed in file names.



With PHP I need to be able to strip these slashes out (replace them with something else). This needs to happen because I have to allow idiots to post images to a web server to be automatically shown online and people (idiots? lets say web challenged) use the "/"... Now here comes the problem:



In Unix (OS X implementation only?) if you read a directory with a file name which contains a "/", it shows up as a colon ":".



If I try to rename the existing file by referring to it as the name PHP reads (with colons) I get an error that the file doesn't exist. If I try to use a "/" in any form - escaped, url encoded, raw - I get the same issues - either the file doesn't exist because the character is wrong, or because it thinks it is going to a new directory and the file still doesn't exist.



Anyone know any way around this? I am no good with shell scripting or I would use PHP to run a script that does it (which I am assuming would work?)



Thanks for any help!



[ 09-11-2002: Message edited by: The Pie Man ]</p>

Comments

  • Reply 1 of 3
    Hoo Boy, that's a toughie. In the future, I would add a simple Javascript or PHP function to the page which user's have access to which would replace the slashes with dashes or prompt the user to use only alphanumeric.



    I'm not good at shell scripting yet, but to replace or fix the filename, encapsulate the name in qutoes when making reference to it using shell commands thusly:



    [code] #rm "Stupid:User:File.jpg"</pre><hr></blockquote>

    Where the original file name was:

    [code] Stupid/User/File.jpg</pre><hr></blockquote>



    So, in order to fix the problem, simply write a quick function that replaces the colons (not slashes) with dashes or strips them out completely.



    Hope this helps...
  • Reply 2 of 3
    Ok, I move to change the name of the topic to "Plagued by permissions, who's the idiot now?"



    Go ahead and ignore this whole topic. I have realized the whole issue stemmed from permission issues.



    To modify a file with a "/" in the name, refer to it in the Unix style with a ":".



    Sorry (mods feel free to delete this topic altogether)
  • Reply 3 of 3
    nah, no point deleting it. there's probably 7 other people who have had, or will have that exact question at some point in the near future.



Sign In or Register to comment.