form input and shell commands

hi all - i’m new to php and also new to this forum.

i have a form that reads in 2 inputs from the user: database name and directory.

DB Name:
Directory:

what i’m trying to do is, based on the user inputs, would for the dbname, execute a mysqldump to backup the database. and for directory, execute a tar -czvf $dir.tar.gz $dir.

i have the following for the tar part:

$directory=$_POST[“dir”];

$output = shell_exec('tar -czvf {$directory}.tar.gz <?php echo $directory; ?>');

however, it’s not executing. please help. thanks

First, is this on a linux system or windows?

I would tell you to hesitate doing it this way as well…

this is linux. thanks

sample input:

dbname: mydb
dir: /var/www/test

[php]$output = shell_exec('tar -czvf {$directory}.tar.gz <?php echo $directory; ?>');[/php]

This won’t work anyway. You are trying to add new php tags while you are still in php. Also, variables are not transposed when in single quotes.

shell_exec may not work for your system. It is HIGHLY dangerous and not something to mess with.

then what do you suggest i do to perform 2 tasks: tar a directory and mysqldump the db based on the 2 inputs?

Try this,
[php]$output = shell_exec(“tar -czvf $directory.tar.gz $directory”);[/php]

I have crons that do my dumps, and if a user needs it they can go to the directory it dumps to. That keeps the people that should have access to it and keeps out the ones that shouldn’t.

still does not work :’(

Replace shell_exec with pass_thru(). As I said your system may not allow it. This isn’t shared hosting is it?

this is my testing vm.

i can execute both from shell and cron them. this is fine and working. i just want to make it more interactive for users to provide what they want to dump and tar.

for some reason, i don’t think the linux shell is able to read the super global variables? it’s only available during the php session?

It should be available if you are running it from a page, from cron or dpi those variables are empty.

$echo $directory

no result.

Try, echo $_SERVER[‘DOCUMENT_ROOT’];

[DOCUMENT_ROOT]

i guess my question should have been:

how do i pass php variable to the shell?

Sponsor our Newsletter | Privacy Policy | Terms of Service