Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

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



Report this thread to moderator Post Follow-up to this message
Old Post
Trevor Barker
09-25-04 01:56 AM


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


HTH;
JW




Report this thread to moderator Post Follow-up to this message
Old Post
Janwillem Borleffs
09-25-04 01:56 AM


Re: File system commands
On 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

Report this thread to moderator Post Follow-up to this message
Old Post
Andy Hassall
09-25-04 01:56 AM


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 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);
?>

Report this thread to moderator Post Follow-up to this message
Old Post
Steven Stern
09-26-04 02:06 PM


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:
> 
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);
> ?>



Report this thread to moderator Post Follow-up to this message
Old Post
Trevor Barker
09-27-04 01:55 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PHP Language archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:23 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.