Problems writing with exec() on Win32

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrew Crowe

    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:\writablefol der\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


  • Pedro Graca

    #2
    Re: Problems writing with exec() on Win32

    Andrew Crowe wrote:[color=blue]
    > Calling exec('echo hello >C:\writablefol der\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.[/color]

    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:\writablefold er\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 :

    Comment

    • anon

      #3
      Re: Problems writing with exec() on Win32

      Pedro Graca wrote...
      [color=blue]
      > Andrew Crowe wrote:
      >[color=green]
      >>Calling exec('echo hello >C:\writablefol der\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.[/color]
      >
      >
      > 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:\writablefold er\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
      >[/color]


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

      so,
      '"%COMSPEC%" /c echo hello > "C:\writablefol der\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.





      Comment

      • Andrew Crowe

        #4
        Re: Problems writing with exec() on Win32

        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\ImageMagi ck-6.0.1-Q16\convert.exe " logo:
        R:\Work\folder\ folder\writable folder\logo.jpg

        copying that directly into the command line runs fine, as does just running
        exec('cmd.exe /c "C:\Program Files\ImageMagi ck-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:c9pve5$ev$ 1@titan.btinter net.com...[color=blue]
        > Pedro Graca wrote...
        >[color=green]
        > > Andrew Crowe wrote:
        > >[color=darkred]
        > >>Calling exec('echo hello >C:\writablefol der\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[/color][/color][/color]
        5[color=blue][color=green][color=darkred]
        > >>rc2 on Win2k IIS using the isapi module.[/color]
        > >
        > >
        > > I can't test this, but I think the redirection in DOS (and all[/color][/color]
        versions[color=blue][color=green]
        > > 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[/color][/color]
        you[color=blue][color=green]
        > > have to call it directly
        > >
        > > exec('command /c echo hello > C:\writablefold er\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
        > >[/color]
        >
        >
        > I broadly agree with this. The correct call should be to %COMSPEC% which,[/color]
        in[color=blue]
        > Win2k, is usually c:\winnt\system 32\cmd.exe
        >
        > so,
        > '"%COMSPEC%" /c echo hello > "C:\writablefol der\text.txt"'
        > (quotes are only needed if there are spaces in the filename, but usually[/color]
        a good[color=blue]
        > 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,[/color]
        but maybe[color=blue]
        > the exec() command interprets it for you.
        >
        >
        >
        >
        >[/color]


        Comment

        • anon

          #5
          Re: Problems writing with exec() on Win32

          Andrew Crowe wrote...
          [color=blue]
          > 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\ImageMagi ck-6.0.1-Q16\convert.exe " logo:
          > R:\Work\folder\ folder\writable folder\logo.jpg
          >
          > copying that directly into the command line runs fine, as does just running
          > exec('cmd.exe /c "C:\Program Files\ImageMagi ck-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?
          >[/color]


          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 '\writablefolde r\', writeable for whom?
          Tried Google?

          Comment

          Working...