Jump to content

How to use the system() argument correctly?


Freedom-n-Democrazy

Recommended Posts

I've been challenging myself to get the system() argument working for the past hour and something. I still can't get it working so I ask for assistance.

 

system ("cp /var/www/http/test.com/products/".$fordir['fordir']."/".$category['category']."/".$_POST['id']." /home/n/Documents/My\ Sites/test.com\ codebase");

 

The error log doesn't say anything, so I suspect that PHP is not even attempting to parse the code.

 

Can anyone spot something that shouldn't be the way it is?

I don't see any syntax errors, but your variables may not be what you expect them to be.  Print out this string, then copy/paste it into a terminal and read the error. 

 

You can also use the additional arguments to system (or exec) to access the returned values and errors.

 

-Dan

I don't see any syntax errors, but your variables may not be what you expect them to be.  Print out this string, then copy/paste it into a terminal and read the error. 

 

You can also use the additional arguments to system (or exec) to access the returned values and errors.

 

Thanks Dan!

 

Heres the info you wanted:

 

<?php
$link = mysql_connect('localhost', 'testusr', 'testdb');
mysql_select_db('testdb', $link);
$query = "select fordir from products where id=".$_POST['id'].""; $result = mysql_query($query); $fordir = mysql_fetch_array($result);
$query = "select category from products where id=".$_POST['id'].""; $result = mysql_query($query); $category = mysql_fetch_array($result);
echo $fordir['fordir'];
$query = "DELETE from `products` where `id` = ".$_POST['id']."";
system ("cp /var/www/http/test.com/products/".$fordir['fordir']."/".$category['category']."/".$_POST['id']." /home/n/Documents/My\ Sites/test.com\ codebase");
echo 'Done.';
mysql_query ($query);
mysql_close($link);
?>

 

The page echo's the correct result fine.

 

 

Backslashes are also special within double quoted strings and need to be escaped.

 

I think your onto something. Could you give me an example of what double quoted strings that are escaped looks like?

Print out this string, then copy/paste it into a terminal and read the error. 

 

Great idea! I did find one problem that would have caused a problem... I left the "-rf" out of the cp command.

I used the command directly into Linux and it worked fine.

 

Now I have a problem where the system() command is not working at all.

You printed it out and copied it into a terminal and it worked?  how is that possible without the proper slashes?

 

I did this:

echo "cp -rf /var/www/http/test.com/products/".$fordir['fordir']."/".$category['category']."/".$_POST['id']." /home/n/Documents/test.com/test.com\ codebase"

I then pasted the output directly into my terminal and the command worked.

 

Double up on your \ characters, make all \ into \\.

Thanks, I tried that but it didn't work. Also, when I used double backslashes in the code above, it echoed exactly the same as with only single backslashes.

 

This is really strange hey! :S

Ok, updated. I tried the whole echo procedure again, and infact I get this output when using both \ and \\:

 

cp -rf /var/www/http/test.com/products/men/belts/69 /home/n/Documents/test.com/test.com\ codebase

 

Note the backslash between test.com and codebase.

 

What I think is happening, is that the path is not braking out. I have no idea how to brake out. Does anyone know where I can learn this in the PHP manual?

"Braking out" has no meaning.

 

What do you WANT this to look like?  This is "correct" as far as I can see because it looks like your destination folder is actually called "test.com codebase".  Is that not your folder name?

You right bro, that is what I want it to look like, sorry, I've only recently discovered the \ with spaces relation with UNIX.

 

Grrrr, then why isn't it working?!

 

I am now using this code for full relevant diagnostics:

<?php
$link = mysql_connect('localhost', 'testusr', 'testpw');
mysql_select_db('testdb', $link);
$query = "select fordir from products where id=".$_POST['id'].""; $result = mysql_query($query); $fordir = mysql_fetch_array($result);
$query = "select category from products where id=".$_POST['id'].""; $result = mysql_query($query); $category = mysql_fetch_array($result);
$query = "DELETE from `products` where `id` = ".$_POST['id']."";
system ("cp -rf /var/www/http/test.com/products/".$fordir['fordir']."/".$category['category']."/".$_POST['id']." /home/n/Documents/test.com\ codebase");
echo "cp -rf /var/www/http/test.com/products/".$fordir['fordir']."/".$category['category']."/".$_POST['id']." /home/n/Documents/test.com\ codebase";
mysql_query ($query);
mysql_close($link);
echo '<BR>';
echo 'Done.';
?>

 

Diagnostic echo:

echo "cp -rf /var/www/http/test.com/products/".$fordir['fordir']."/".$category['category']."/".$_POST['id']." /home/n/Documents/test.com\ codebase";

This outputs fine:

cp -rf /var/www/http/test.com/products/men/belts/1 /home/n/Documents/test.com\ codebase

... and works fine when I paste it over into a bash terminal - copying the desired folder into the desired destination without problem.

 

There is no error in my log.

 

I really don't understand why this won't work?!

FYI:  Instead of:

 

system ("cp -rf /var/www/http/test.com/products/".$fordir['fordir']."/".$category['category']."/".$_POST['id']." /home/n/Documents/test.com\ codebase");

 

$retval = `cp -rf /var/www/http/test.com/products/".$fordir['fordir']."/".$category['category']."/".$_POST['id']." /home/n/Documents/test.com\ codebase`;
echo "Result:  $retval";

 

One thing to look at would be using the path to cp which is probably /bin/cp.  In your shell try "which cp".

 

 

 

 

I get:

PHP Parse error:  syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

for:

$retval = `cp -rf /var/www/http/test.com/products/".$fordir['fordir']."/".$category['category']."/".$_POST['id']." /home/n/Documents/test.com\ codebase`;

Try this:

 

$cmd = "cp -rf /var/www/http/test.com/products/{$fordir['fordir']}/{$category['category']}/{$_POST['id']} /home/n/Documents/test.com codebase";
echo "Command is: $cmd 
";
$retval = shell_exec($cmd);
echo $retval;

I used this:

 

<?php

$link = mysql_connect('localhost', 'testusr', 'testpw');

mysql_select_db('testdb', $link);

$query = "select fordir from products where id=".$_POST['id'].""; $result = mysql_query($query); $fordir = mysql_fetch_array($result);

$query = "select category from products where id=".$_POST['id'].""; $result = mysql_query($query); $category = mysql_fetch_array($result);

$query = "DELETE from `products` where `id` = ".$_POST['id']."";

$cmd = "cp -rf /var/www/http/test.com/products/{$fordir['fordir']}/{$category['category']}/{$_POST['id']} /home/n/Documents/test.com codebase";

echo "command is: $cmd <BR>";
$retval = shell_exec($cmd);
echo $retval;
echo "result: $retval";
mysql_query ($query);

mysql_close($link);

echo 'Done.';

?>

 

... and got this in my browser:

 

command is: cp -rf /var/www/http/test.com/products/men/belts/69 /home/n/Documents/test.com codebase

result: Done.

 

The directory didn't copy.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.