469,628 Members | 1,031 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,628 developers. It's quick & easy.

Problems writing with exec() on Win32

Hi guys,

I'm having trouble executing commands using exec() or system() that need to
write files.

I've set the permissions on CMD.EXE but I can't use exec() to write any
files, even if the folder I'm writing to has write access to IUSER account
and PHP can write to the folder directly.

eg. Calling exec('echo hello >C:\writablefolder\text.txt') does nothing,
but if I type that same command into a shell prompt it works fine.

Also just calling exec('echo hello') returns "hello" so it's definatly
executing the command.
Does anyone know how I can fix this? What I'm trying to do is use
ImageMagick to do some image manipulation. Version of PHP I'm using is 5
rc2 on Win2k IIS using the isapi module.

Thanks

Andrew
Jul 17 '05 #1
4 2853
Andrew Crowe wrote:
Calling exec('echo hello >C:\writablefolder\text.txt') does nothing,
but if I type that same command into a shell prompt it works fine.

Also just calling exec('echo hello') returns "hello" so it's definatly
executing the command.
Does anyone know how I can fix this? What I'm trying to do is use
ImageMagick to do some image manipulation. Version of PHP I'm using is 5
rc2 on Win2k IIS using the isapi module.


I can't test this, but I think the redirection in DOS (and all versions
of Windows) is not a command-like thing, but rather an interpreted
thing (sorry ... can't explain any better)

I mean: only command.com (or cmd.exe or whatever) can redirect, and you
have to call it directly

exec('command /c echo hello > C:\writablefolder\text.txt');

I just put this /c here from memory. It is probably wrong :)
Check with "command /?" the options available to command.com

.... You may need extra quotes there too

HTH
--
USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :
Jul 17 '05 #2
Pedro Graca wrote...
Andrew Crowe wrote:
Calling exec('echo hello >C:\writablefolder\text.txt') does nothing,
but if I type that same command into a shell prompt it works fine.

Also just calling exec('echo hello') returns "hello" so it's definatly
executing the command.
Does anyone know how I can fix this? What I'm trying to do is use
ImageMagick to do some image manipulation. Version of PHP I'm using is 5
rc2 on Win2k IIS using the isapi module.

I can't test this, but I think the redirection in DOS (and all versions
of Windows) is not a command-like thing, but rather an interpreted
thing (sorry ... can't explain any better)

I mean: only command.com (or cmd.exe or whatever) can redirect, and you
have to call it directly

exec('command /c echo hello > C:\writablefolder\text.txt');

I just put this /c here from memory. It is probably wrong :)
Check with "command /?" the options available to command.com

.... You may need extra quotes there too

I broadly agree with this. The correct call should be to %COMSPEC% which, in
Win2k, is usually c:\winnt\system32\cmd.exe

so,
'"%COMSPEC%" /c echo hello > "C:\writablefolder\text.txt"'
(quotes are only needed if there are spaces in the filename, but usually a good
idea anyway)
Note that command.com is totally different in WinNT/2k/XP - don't use it.
at the command prompt, type- echo %comspec% and %comspec% /?

%COMSPEC% should always be in the environment variables. I don't know, but maybe
the exec() command interprets it for you.

Jul 17 '05 #3
I've tried using cmd /c but it still won't write any files.
this is what I'm actually trying to execute:
cmd.exe /c "C:\Program Files\ImageMagick-6.0.1-Q16\convert.exe" logo:
R:\Work\folder\folder\writablefolder\logo.jpg

copying that directly into the command line runs fine, as does just running
exec('cmd.exe /c "C:\Program Files\ImageMagick-6.0.1-Q16\convert.exe"') (it
returns the help text for image magick), is there anything else I could try
to get this working?
Thanks,

Andrew

"anon" <me@privacy.net> wrote in message
news:c9*********@titan.btinternet.com...
Pedro Graca wrote...
> Andrew Crowe wrote:
>
>>Calling exec('echo hello >C:\writablefolder\text.txt') does nothing,
>>but if I type that same command into a shell prompt it works fine.
>>
>>Also just calling exec('echo hello') returns "hello" so it's definatly
>>executing the command.
>>
>>
>>Does anyone know how I can fix this? What I'm trying to do is use
>>ImageMagick to do some image manipulation. Version of PHP I'm using is 5 >>rc2 on Win2k IIS using the isapi module. >
>
> I can't test this, but I think the redirection in DOS (and all versions > of Windows) is not a command-like thing, but rather an interpreted
> thing (sorry ... can't explain any better)
>
> I mean: only command.com (or cmd.exe or whatever) can redirect, and you > have to call it directly
>
> exec('command /c echo hello > C:\writablefolder\text.txt');
>
> I just put this /c here from memory. It is probably wrong :)
> Check with "command /?" the options available to command.com
>
> .... You may need extra quotes there too
>

I broadly agree with this. The correct call should be to %COMSPEC% which,

in Win2k, is usually c:\winnt\system32\cmd.exe

so,
'"%COMSPEC%" /c echo hello > "C:\writablefolder\text.txt"'
(quotes are only needed if there are spaces in the filename, but usually a good idea anyway)
Note that command.com is totally different in WinNT/2k/XP - don't use it.
at the command prompt, type- echo %comspec% and %comspec% /?

%COMSPEC% should always be in the environment variables. I don't know, but maybe the exec() command interprets it for you.


Jul 17 '05 #4
Andrew Crowe wrote...
I've tried using cmd /c but it still won't write any files.
this is what I'm actually trying to execute:
cmd.exe /c "C:\Program Files\ImageMagick-6.0.1-Q16\convert.exe" logo:
R:\Work\folder\folder\writablefolder\logo.jpg

copying that directly into the command line runs fine, as does just running
exec('cmd.exe /c "C:\Program Files\ImageMagick-6.0.1-Q16\convert.exe"') (it
returns the help text for image magick), is there anything else I could try
to get this working?

Well, that's a whole different kettle of fish from an echo command.
First question - does the 'echo>file.txt' example work??
If not, then you need to look at permissions.

I would have thought that a command that isn't 'in the shell' (echo, dir, etc)
would not need the command interpreter cmd.exe

Other thoughts:

cmd.exe /c START c:/progra~1/imag
a) use START, and b) use short filenames (dir /x)

Check your IUSR_? and IWAM_? permissions on cmd.exe and ImageMagick and the
picture/folder. It sounds like the first two are OK (if you get help output).
When you say '\writablefolder\', writeable for whom?
Tried Google?

Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Craig Hurcum | last post: by
14 posts views Thread by Jim Hubbard | last post: by
3 posts views Thread by Dmitry Bond | last post: by
reply views Thread by Vidyasagara Guntaka | last post: by
3 posts views Thread by Thomas Heller | last post: by
7 posts views Thread by JahMic | last post: by
1 post views Thread by Matimus | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.