Re: Spaces in filenames [message #173703 is a reply to message #173701] |
Sat, 30 April 2011 06:57 |
Eli the Bearded
Messages: 22 Registered: April 2011
Karma:
|
Junior Member |
|
|
In comp.lang.php, Tim Streater <timstreater(at)waitrose(dot)com> wrote:
> When my app starts up, it discovers where it has been installed using:
>
> $instdir = dirname (__FILE__);
>
> Now, I have no control over where this might be in the file system, and
> the user may well put it somewhere such that the path contains spaces.
> I'm passing this round the app, and in the fullness of time may be doing
> things like:
>
> $fp = fopen ($instdir . "/wiggy", "r");
Why? Just chdir() and use relative filenames. You'll save yourself
a boatload of headache.
$instdir = dirname (__FILE__);
chdir($instdir);
$fp = fopen ("./wiggy", "r");
> But what's worse, I'll also be passing $instdir to shell scripts via
> exec ().
Most people just do a half-assed job because who would ever:
# mkdir "rm -rf *;" && cd "rm -rf *;" && tar xzf /tmp/INSTALLPACKAGE.tgz
(For one thing, the "./configure && make" will typically faily
horribly.)
But if you aren't willing to cope with that, you can make things
a damn-sight easier by simply not caring where you are installed.
If you can treat it like it is your own chroot() environment and
never need to know the actual name of the install directory, you
won't care what craziness someone tries.
> 2) leave $instdir as-is, but use escapeshellarg on it whenever I need to
> pass it through exec().
Wait, you know how to escape shell characters and you are thinking
about not doing it?
> Which is the better approach?
It's a cruel world out there. Try to be prepared.
> "That excessive bail ought not to be required, nor excessive fines imposed,
> nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
Don't forget the directories with control characters in them that
hide how messed up they are to the naked eye. I love those.
mkdir "<!--;rm -rf *;-->^H..17x^H..longnstalldirname" && cd \<*name && pwd
Elijah
------
protip: type each of the the ^Hs as <ctrl-V><ctrl-H>
|
|
|