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?
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):
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)
>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.
>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.
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" <sdsternNOSPAMH...@NOSPAMHEREmindspring.com> wrote in message
> On Fri, 24 Sep 2004 21:53:54 GMT (more or less), "Trevor Barker" > <trevor.bark...@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.