471,092 Members | 1,166 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,092 software developers and data experts.

Escaping file names?

Hi,

I'm using PHP 5 on Linux. I have a function that checks for file type
by doing the following:

$cmd = "file $p_input_file";
exec("$cmd 2>&1", $output, $return);

However if the file "$p_input_file" contains spaces, the command
doesn't work properly (I would need to insert a "\" before the space
for it to work). I figure there are probably some other characters in
the file name that would screw up the command. Is there a predefined
way of escaping the file names or do I need to just do a generic
search and replace on all the special characters I can think of?

Thanks, - Dave
Aug 4 '08 #1
4 3561
Hi,

la***********@zipmail.com wrote:
$cmd = "file $p_input_file";
exec("$cmd 2>&1", $output, $return);

However if the file "$p_input_file" contains spaces, the command
doesn't work properly (I would need to insert a "\" before the space
for it to work). I figure there are probably some other characters in
the file name that would screw up the command. Is there a predefined
way of escaping the file names or do I need to just do a generic
search and replace on all the special characters I can think of?
escapeshellarg(). It's even in the same manual section as exec() :-)
-hwh
Aug 4 '08 #2
..oO(la***********@zipmail.com)
>I'm using PHP 5 on Linux. I have a function that checks for file type
by doing the following:

$cmd = "file $p_input_file";
exec("$cmd 2>&1", $output, $return);

However if the file "$p_input_file" contains spaces, the command
doesn't work properly (I would need to insert a "\" before the space
for it to work). I figure there are probably some other characters in
the file name that would screw up the command. Is there a predefined
way of escaping the file names or do I need to just do a generic
search and replace on all the special characters I can think of?
escapeshellarg()
escapeshellcmd()

Micha
Aug 4 '08 #3
On Aug 4, 9:36*am, Michael Fesser <neti...@gmx.dewrote:
.oO(laredotorn...@zipmail.com)
I'm using PHP 5 on Linux. *I have a function that checks for file type
by doing the following:
* * * * * * * *$cmd = "file $p_input_file";
* * * * * * * *exec("$cmd 2>&1", $output, $return);
However if the file "$p_input_file" contains spaces, the command
doesn't work properly (I would need to insert a "\" before the space
for it to work). *I figure there are probably some other characters in
the file name that would screw up the command. *Is there a predefined
way of escaping the file names or do I need to just do a generic
search and replace on all the special characters I can think of?

escapeshellarg()
escapeshellcmd()

Micha
Thanks but I think I'm still doing something wrong. I have

$p_input_file = "/home/me/Toad Getting Started
Guide.pdf";
$cmd = escapeshellcmd("file $p_input_file");
print "cmd: $cmd<BR>\n"; # outputs 'cmd: file /home/
me/Toad Getting Started Guide.pdf'

Shouldn't the command print out as

cmd: file /home/laredotornado/Toad\ Getting\ Started\ Guide.pdf

? - Dave
Aug 4 '08 #4
*** la***********@zipmail.com escribió/wrote (Mon, 4 Aug 2008 12:23:06
-0700 (PDT)):
$p_input_file = "/home/me/Toad Getting Started
Guide.pdf";
$cmd = escapeshellcmd("file $p_input_file");
print "cmd: $cmd<BR>\n"; # outputs 'cmd: file /home/
me/Toad Getting Started Guide.pdf'
Try:

$cmd = 'file ' . escapeshellarg($p_input_file);
I've never really understood what escapeshellcmd() is supposed to do.
Apparently, it tries to prevent user input from doing harm, but it doesn't
care about making the command actually work.

--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor en cubitos: http://www.demogracia.com
--
Aug 5 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Dave Moore | last post: by
5 posts views Thread by Aloysio Figueiredo | last post: by
8 posts views Thread by felciano | last post: by
4 posts views Thread by agarwalpiyush | last post: by
5 posts views Thread by Troot | last post: by
8 posts views Thread by Frank Rizzo | last post: by
3 posts views Thread by Taras_96 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.