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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?!

Link to comment
Share on other sites

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".

 

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.