File system commands

Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default File system commands

    Hi,

    I've been trying to work out how to issue file system commands from a PHP
    script to Redhat Linux. I want to be able to delete files and directories,
    and copy files. I thought that using 'exec', 'system', or 'passthru' would
    be the answer but although they don't produce errors (thereby indicating
    that they work), the don't actually carry out the command. Can anyone
    suggest how I should do it please?

    Thanks
    Trev


    Trevor Barker Guest

  2. Similar Questions and Discussions

    1. piped system commands
      deb wrote: Try changing the $2 to \$2. Perl is interpolating $2 before it gets to bash, so bash sees "/bin/awk '{print }'". -- Andrew Gaffney...
    2. perl interactive system commands
      rhlinux wrote: I've tried just about every method you can think of to get this to work correctly. Unfortunately, passwd is coded to only accept...
    3. Running system commands
      Hi all, I was wondering, if anyone can help me with running system commands from within php. Actually I have a script which deletes users from my...
    4. system commands
      i know that system commands can be run using the system("command") function, but... some commands, such as mysqldump, require additional input -...
    5. System() works on /usr/sbin commands
      hello, i was using system function to invoke useradd command but it doesn't work. well it works for all the commands but not those in /usr/sbin....
  3. #2

    Default Re: File system commands

    Trevor Barker wrote:
    > I've been trying to work out how to issue file system commands from a
    > PHP script to Redhat Linux. I want to be able to delete files and
    > directories, and copy files. I thought that using 'exec', 'system',
    > or 'passthru' would be the answer but although they don't produce
    > errors (thereby indicating that they work), the don't actually carry
    > out the command. Can anyone suggest how I should do it please?
    >
    You can only execute file system commands when the files and directories are
    either chmod 777 or writeable by Apache.

    I'm sure that when you run the following code it will print 1, indicating
    that an error occurred (replace somefile with a file that actually present
    on your filesystem):

    <?
    exec ("cp somefile anotherfile", $output, $result);
    print $result;
    ?>

    Furthermore, it's more efficient to use native PHP functions instead of
    passing system commands through the exec/passthru/etc functions.

    Per example:

    To delete a file, use unlink(); to remove a dir use rmdir(); to copy a file
    use copy(). See the manual for more info about these functions.

    What you should do is the following:

    1. Create a sandbox directory which you chmod to 777
    2. Use the suggested functions in this sandbox directory only
    3. Start reading the manual ([url]http://nl.php.net/manual/en/ref.filesystem.php[/url])


    HTH;
    JW



    Janwillem Borleffs Guest

  4. #3

    Default Re: File system commands

    On Fri, 24 Sep 2004 21:53:54 GMT, "Trevor Barker" <trevor.barker7@ntlworld.com>
    wrote:
    >I've been trying to work out how to issue file system commands from a PHP
    >script to Redhat Linux. I want to be able to delete files and directories,
    >and copy files. I thought that using 'exec', 'system', or 'passthru' would
    >be the answer but although they don't produce errors (thereby indicating
    >that they work), the don't actually carry out the command. Can anyone
    >suggest how I should do it please?
    PHP has various PHP-native filesystem functions which are arguably better
    choices than spawning shells and opening potential command injection,
    performance and portability issues.

    See the Filesystem Functions chapter of the PHP manual for details.

    --
    Andy Hassall / <andy@andyh.co.uk> / <http://www.andyh.co.uk>
    <http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
    Andy Hassall Guest

  5. #4

    Default Re: File system commands

    On Fri, 24 Sep 2004 21:53:54 GMT (more or less), "Trevor Barker"
    <trevor.barker7@ntlworld.com> wrote:
    >Hi,
    >
    >I've been trying to work out how to issue file system commands from a PHP
    >script to Redhat Linux. I want to be able to delete files and directories,
    >and copy files. I thought that using 'exec', 'system', or 'passthru' would
    >be the answer but although they don't produce errors (thereby indicating
    >that they work), the don't actually carry out the command. Can anyone
    >suggest how I should do it please?
    >
    >Thanks
    >Trev
    >
    Here's a simple script that searches a given directory and deletes files more
    than 2 days old. Look up file functions on php.net.

    #! /bin/php
    <?php

    // calculate two days ago

    $now = time();
    $old = $now - (60*60*24*2);
    echo "<hr>\n\nDelete old report files ",date("Y-m-d H:i:s"),"<br>\n";
    echo "Using ", date("Y-m-d H:i:s",$old),"<br>\n";

    // make a list of files in tmp

    $dir = '/www/htdocs/admin/tmp/';
    if ($handle = opendir($dir)) {
    while (false !== ($file = readdir($handle))) {
    if ($file == ".") continue;
    if ($file == "..") continue;
    $fname = $dir.$file;
    if (filemtime($fname) < $old):
    echo "deleting ",$fname,": ",date("Y-m-d
    H:i:s",filemtime($fname)),"<br>\n";
    unlink($fname); ;
    endif;
    }
    }

    closedir($handle);
    ?>
    Steven Stern Guest

  6. #5

    Default Re: File system commands

    Thanks guys, I think ownership is the key to the problem i've been having
    but I will look at the internal funnctions too, I didnt realise they were
    there.


    "Steven Stern" <sdsternNOSPAMHERE@NOSPAMHEREmindspring.com> wrote in message
    news:o3edl01gj0gpgac2b6b3e6fsu2c6nt518a@4ax.com...
    > On Fri, 24 Sep 2004 21:53:54 GMT (more or less), "Trevor Barker"
    > <trevor.barker7@ntlworld.com> wrote:
    >
    > >Hi,
    > >
    > >I've been trying to work out how to issue file system commands from a PHP
    > >script to Redhat Linux. I want to be able to delete files and
    directories,
    > >and copy files. I thought that using 'exec', 'system', or 'passthru'
    would
    > >be the answer but although they don't produce errors (thereby indicating
    > >that they work), the don't actually carry out the command. Can anyone
    > >suggest how I should do it please?
    > >
    > >Thanks
    > >Trev
    > >
    >
    > Here's a simple script that searches a given directory and deletes files
    more
    > than 2 days old. Look up file functions on php.net.
    >
    > #! /bin/php
    > <?php
    >
    > // calculate two days ago
    >
    > $now = time();
    > $old = $now - (60*60*24*2);
    > echo "<hr>\n\nDelete old report files ",date("Y-m-d H:i:s"),"<br>\n";
    > echo "Using ", date("Y-m-d H:i:s",$old),"<br>\n";
    >
    > // make a list of files in tmp
    >
    > $dir = '/www/htdocs/admin/tmp/';
    > if ($handle = opendir($dir)) {
    > while (false !== ($file = readdir($handle))) {
    > if ($file == ".") continue;
    > if ($file == "..") continue;
    > $fname = $dir.$file;
    > if (filemtime($fname) < $old):
    > echo "deleting ",$fname,": ",date("Y-m-d
    > H:i:s",filemtime($fname)),"<br>\n";
    > unlink($fname); ;
    > endif;
    > }
    > }
    >
    > closedir($handle);
    > ?>

    Trevor Barker Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139