running shell command within php

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andreas Paasch

    #1

    running shell command within php

    According to the manual for PHP, I should be able to run a shell command
    within php.

    I'm trying to copy some php files from one location to another one using
    exec() but fail.

    Code:
    if(exec("/bin/cp -f /home/web/public/min-pizza/master/
    /home/web/public/min-pizza/$postnr/$folder/") == TRUE)
    echo "... masterfiles copied ...";
    $postnr = 2720
    $folder = 38747117-robertos

    They are created dynamically and must stay as they are, it's a standard
    format for creating folders and consequently copying files.

    But can't do it ... I've read and read, found no solution so far.

    Any help is highly welcome.
    --
    Registeret Linux user #292411
  • vAm

    #2
    Re: running shell command within php

    Andreas Paasch wrote:[color=blue]
    > According to the manual for PHP, I should be able to run a shell command
    > within php.
    >
    > I'm trying to copy some php files from one location to another one using
    > exec() but fail.
    >
    >
    Code:
    > if(exec("/bin/cp -f /home/web/public/min-pizza/master/
    >                 /home/web/public/min-pizza/$postnr/$folder/") == TRUE)
    >         echo "... masterfiles copied ...";
    >
    >
    > $postnr = 2720
    > $folder = 38747117-robertos
    >
    > They are created dynamically and must stay as they are, it's a standard
    > format for creating folders and consequently copying files.
    >
    > But can't do it ... I've read and read, found no solution so far.
    >
    > Any help is highly welcome.[/color]

    I'm a newbie to PHP. Well, did you try the function system()? Same
    syntax as in C.

    vAm

    Comment

    • Michael Fuhr

      #3
      Re: running shell command within php

      Andreas Paasch <Andreas@Paasch .Net> writes:
      [color=blue]
      > According to the manual for PHP, I should be able to run a shell command
      > within php.[/color]

      Correct, but this ability can be restricted by settings in php.ini,
      such as safe_mode, safe_mode_exec_ dir, and disable_functio ns.
      [color=blue]
      > I'm trying to copy some php files from one location to another one using
      > exec() but fail.
      >
      > if(exec("/bin/cp -f /home/web/public/min-pizza/master/
      > /home/web/public/min-pizza/$postnr/$folder/") == TRUE)
      > echo "... masterfiles copied ...";[/color]

      It looks like you're trying to copy directories, and on most Unix-like
      systems you need to give cp a recursive option (-r or -R) to do
      that. Read the manual page for cp and make sure your cp command
      works from the shell prompt before trying to make it work in PHP.

      Also, exec() returns the last line of the command's output, not a
      status indicating success or failure. A successful cp command
      doesn't usually produce any output, so exec()'s return value will
      be an empty string, which evaluates to FALSE. If you want to get
      the command's exit status then use the three-argument form of exec(),
      which stores the status in the third argument. The Unix convention
      is that an exit status of 0 means success and anything else means
      failure, so be sure to take that into account.

      --
      Michael Fuhr

      Comment

      • Andreas Paasch

        #4
        Re: running shell command within php

        Andreas Paasch wrote:
        [color=blue]
        > According to the manual for PHP, I should be able to run a shell command
        > within php.
        >
        > I'm trying to copy some php files from one location to another one using
        > exec() but fail.
        >
        >
        Code:
        > if(exec("/bin/cp -f /home/web/public/min-pizza/master/
        >                 /home/web/public/min-pizza/$postnr/$folder/") == TRUE)
        >         echo "... masterfiles copied ...";
        >
        >
        > $postnr = 2720
        > $folder = 38747117-robertos
        >
        > They are created dynamically and must stay as they are, it's a standard
        > format for creating folders and consequently copying files.
        >
        > But can't do it ... I've read and read, found no solution so far.
        >
        > Any help is highly welcome.[/color]


        Actually, I stumpled over the obvious - PHP function copy().
        Didn't even get that to work, however in the notes a nice little function
        was written so I tested that - works like a charm now.

        I don't have any PHP limitations set, so that's not the issue, I have tried
        system() too, to no avail. It must somehow be related to my syntaxing.

        Now, about the recursive option for copy, it's only needed if I want
        subdirectories copied, which isn't the case, I only wanted all files from
        /master/ to be copied to the newly created folder.

        But, the function mentioned works so for now I go on with my coding, my
        optimizing it all is a later chapter ....

        Thanks anyways, always helps to get some new eyes on problems.

        /Andreas

        --
        Registeret Linux user #292411

        Comment

        Working...