Ask a Question related to PHP Development, Design and Development.
-
Trevor Barker #1
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
-
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... -
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... -
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... -
system commands
i know that system commands can be run using the system("command") function, but... some commands, such as mysqldump, require additional input -... -
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.... -
Janwillem Borleffs #2
Re: File system commands
Trevor Barker wrote:
You can only execute file system commands when the files and directories are> 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?
>
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
-
Andy Hassall #3
Re: File system commands
On Fri, 24 Sep 2004 21:53:54 GMT, "Trevor Barker" <trevor.barker7@ntlworld.com>
wrote:
PHP has various PHP-native filesystem functions which are arguably better>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?
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
-
Steven Stern #4
Re: File system commands
On Fri, 24 Sep 2004 21:53:54 GMT (more or less), "Trevor Barker"
<trevor.barker7@ntlworld.com> wrote:
Here's a simple script that searches a given directory and deletes files more>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
>
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
-
Trevor Barker #5
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...directories,> 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 andwould> >and copy files. I thought that using 'exec', 'system', or 'passthru'more>> >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> 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



Reply With Quote

