For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > September 2004 > File system commands









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author File system commands
Trevor Barker

2004-09-24, 8:56 pm

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


Janwillem Borleffs

2004-09-24, 8:56 pm

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 (http://nl.php.net/manual/en/ref.filesystem.php)


HTH;
JW



Andy Hassall

2004-09-24, 8:56 pm

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
Steven Stern

2004-09-26, 9:06 am

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

2004-09-26, 8:55 pm

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:
>
directories,[color=darkred]
would[color=darkred]
>
> 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);
> ?>



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com