Code Comments
Programming Forum and web based access to our favorite programming groups.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
Post Follow-up to this messageTrevor 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 (http://nl.php.net/manual/en/ref.filesystem.php)
HTH;
JW
Post Follow-up to this messageOn Fri, 24 Sep 2004 21:53:54 GMT, "Trevor Barker" <trevor.barker7@ntlworld.c om> 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
Post Follow-up to this messageOn 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 mor
e
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);
?>
Post Follow-up to this messageThanks 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:
>
directories,
would
>
> 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);
> ?>
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.