Freedom-n-Democrazy Posted September 22, 2011 Share Posted September 22, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/247642-how-to-use-the-system-argument-correctly/ Share on other sites More sharing options...
trq Posted September 22, 2011 Share Posted September 22, 2011 Why aren't you using php's copy function? Quote Link to comment https://forums.phpfreaks.com/topic/247642-how-to-use-the-system-argument-correctly/#findComment-1271696 Share on other sites More sharing options...
Freedom-n-Democrazy Posted September 22, 2011 Author Share Posted September 22, 2011 Didn't know it existed. Well, can you spot anything? Quote Link to comment https://forums.phpfreaks.com/topic/247642-how-to-use-the-system-argument-correctly/#findComment-1271698 Share on other sites More sharing options...
ManiacDan Posted September 22, 2011 Share Posted September 22, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/247642-how-to-use-the-system-argument-correctly/#findComment-1271704 Share on other sites More sharing options...
trq Posted September 22, 2011 Share Posted September 22, 2011 Backslashes are also special within double quoted strings and need to be escaped. Quote Link to comment https://forums.phpfreaks.com/topic/247642-how-to-use-the-system-argument-correctly/#findComment-1271705 Share on other sites More sharing options...
Freedom-n-Democrazy Posted September 22, 2011 Author Share Posted September 22, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/247642-how-to-use-the-system-argument-correctly/#findComment-1271707 Share on other sites More sharing options...
Freedom-n-Democrazy Posted September 22, 2011 Author Share Posted September 22, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/247642-how-to-use-the-system-argument-correctly/#findComment-1271739 Share on other sites More sharing options...
ManiacDan Posted September 22, 2011 Share Posted September 22, 2011 You printed it out and copied it into a terminal and it worked? how is that possible without the proper slashes? Double up on your \ characters, make all \ into \\. Quote Link to comment https://forums.phpfreaks.com/topic/247642-how-to-use-the-system-argument-correctly/#findComment-1271751 Share on other sites More sharing options...
Freedom-n-Democrazy Posted September 22, 2011 Author Share Posted September 22, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/247642-how-to-use-the-system-argument-correctly/#findComment-1271764 Share on other sites More sharing options...
Freedom-n-Democrazy Posted September 22, 2011 Author Share Posted September 22, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/247642-how-to-use-the-system-argument-correctly/#findComment-1271775 Share on other sites More sharing options...
ManiacDan Posted September 22, 2011 Share Posted September 22, 2011 "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? Quote Link to comment https://forums.phpfreaks.com/topic/247642-how-to-use-the-system-argument-correctly/#findComment-1271790 Share on other sites More sharing options...
Freedom-n-Democrazy Posted September 23, 2011 Author Share Posted September 23, 2011 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?! Quote Link to comment https://forums.phpfreaks.com/topic/247642-how-to-use-the-system-argument-correctly/#findComment-1271891 Share on other sites More sharing options...
gizmola Posted September 23, 2011 Share Posted September 23, 2011 Why aren't you using php's copy function? Use the right tool for the right job. Quote Link to comment https://forums.phpfreaks.com/topic/247642-how-to-use-the-system-argument-correctly/#findComment-1271895 Share on other sites More sharing options...
gizmola Posted September 23, 2011 Share Posted September 23, 2011 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". Quote Link to comment https://forums.phpfreaks.com/topic/247642-how-to-use-the-system-argument-correctly/#findComment-1271897 Share on other sites More sharing options...
Freedom-n-Democrazy Posted September 23, 2011 Author Share Posted September 23, 2011 I will actually be using 'mv' in the finished version. I'm just using 'cp' for now so I don't have to move folders back and forward with testing.... when I get the bloody thing working that is! :\ Quote Link to comment https://forums.phpfreaks.com/topic/247642-how-to-use-the-system-argument-correctly/#findComment-1271898 Share on other sites More sharing options...
Freedom-n-Democrazy Posted September 23, 2011 Author Share Posted September 23, 2011 One thing to look at would be using the path to cp which is probably /bin/cp. In your shell try "which cp". GREAT idea, but didn't work. Quote Link to comment https://forums.phpfreaks.com/topic/247642-how-to-use-the-system-argument-correctly/#findComment-1271900 Share on other sites More sharing options...
gizmola Posted September 23, 2011 Share Posted September 23, 2011 One thing to look at would be using the path to cp which is probably /bin/cp. In your shell try "which cp". GREAT idea, but didn't work. What about the shell_exec output? Quote Link to comment https://forums.phpfreaks.com/topic/247642-how-to-use-the-system-argument-correctly/#findComment-1271902 Share on other sites More sharing options...
Freedom-n-Democrazy Posted September 23, 2011 Author Share Posted September 23, 2011 The think the absence of the operation in error.log suggests PHP is ignoring the line all together? Quote Link to comment https://forums.phpfreaks.com/topic/247642-how-to-use-the-system-argument-correctly/#findComment-1271903 Share on other sites More sharing options...
Freedom-n-Democrazy Posted September 23, 2011 Author Share Posted September 23, 2011 shell_exec ("...command..."); didn't work either and still nothing in error.log. Quote Link to comment https://forums.phpfreaks.com/topic/247642-how-to-use-the-system-argument-correctly/#findComment-1271904 Share on other sites More sharing options...
Freedom-n-Democrazy Posted September 23, 2011 Author Share Posted September 23, 2011 Perhaps my PHP has a setting switched off that allows passthru of commands to the system? EDIT: No. Quote Link to comment https://forums.phpfreaks.com/topic/247642-how-to-use-the-system-argument-correctly/#findComment-1271911 Share on other sites More sharing options...
gizmola Posted September 23, 2011 Share Posted September 23, 2011 I pasted you code to use to get the return value of the command. $retval = `command` is the same as $retval = shell_exec(command). I want you to run that so we can see if there's output. Quote Link to comment https://forums.phpfreaks.com/topic/247642-how-to-use-the-system-argument-correctly/#findComment-1271912 Share on other sites More sharing options...
Freedom-n-Democrazy Posted September 23, 2011 Author Share Posted September 23, 2011 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`; Quote Link to comment https://forums.phpfreaks.com/topic/247642-how-to-use-the-system-argument-correctly/#findComment-1271915 Share on other sites More sharing options...
gizmola Posted September 23, 2011 Share Posted September 23, 2011 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; Quote Link to comment https://forums.phpfreaks.com/topic/247642-how-to-use-the-system-argument-correctly/#findComment-1271916 Share on other sites More sharing options...
Freedom-n-Democrazy Posted September 23, 2011 Author Share Posted September 23, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/247642-how-to-use-the-system-argument-correctly/#findComment-1271918 Share on other sites More sharing options...
trq Posted September 23, 2011 Share Posted September 23, 2011 You need to escape spacing in file/directory names on *nix. Quote Link to comment https://forums.phpfreaks.com/topic/247642-how-to-use-the-system-argument-correctly/#findComment-1271920 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.