473,387 Members | 1,463 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 3739
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Dave Moore | last post by:
Hi All, Can anybody point me to a FAQ or similar that describes what all this stuff is about please?. I'm interfacing with a MySQL database if that's relavent. I've read a couple of books which...
5
by: Aloysio Figueiredo | last post by:
I need to replace every ocurrence of '/' in s by '\/' in order to create a file named s. My first attempt was: s = '\/'.join(s.split('/')) but it doesn't work: >>> s = 'a/b' >>> s =...
8
by: felciano | last post by:
Hi -- I am trying to use the csv module to parse a column of values containing comma-delimited values with unusual escaping: AAA, BBB, CCC (some text, right here), DDD I want this to come...
6
by: Joe Price | last post by:
Hi all I've formatted one of my xml files for viewing through a web browser using xsl. It works fine in Internet Explorer, however when I use Netscape6 or Opera to view the same page the...
4
by: agarwalpiyush | last post by:
Hello, I am going nuts with trying to get the following to work: This is what I intend to do: I have a line in /etc/syslog.conf which I need to delete based on ip-address provided to me in a...
5
by: Troot | last post by:
Hi All, I was wondering if someone could clear this up for me. I have constructed a sample for a bigger problem I'm having. So, given the xml file: <?xml-stylesheet href="test.xsl"...
8
by: Frank Rizzo | last post by:
Hello, I'd like to have the following structure in my XML file <lname, _fname, _minit> <status>it is all good</status> </lname, _fname, _minit> But apparently, there is a problem with...
4
by: Jon | last post by:
Hi, I used XslCompiledTransform with the following Xsl file. The <xsl:text disable-output-escaping="yes"does not work when using XslCompiledTransform to do the trnasform (namely the output...
3
by: Taras_96 | last post by:
Hi everyone, I'm having a bit of trouble understanding the purpose of escaping nulls, and the use of addcslashes. Firstly, the manual states that: "Strictly speaking, MySQL requires only...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.