Linux like script level exec command on windows xampp

Problems with the Windows version of XAMPP, questions, comments, and anything related.

Linux like script level exec command on windows xampp

Postby optimisticwalker » 13. September 2018 11:25

How to execute windows command in a PHP script in a similar way we do on linux?

For example, to copy file from one directory to another we can execute the linux find & cp command directly into a PHP script like,

Code: Select all
<?php
exec("find E:/xampp/htdocs/site.com/image/ -name test.png -exec cp  {} E:/xampp/htdocs/site.com/thumb/\;");
?>


On the other hand to copy file through windows command prompt we use,

Code: Select all
E:\xampp\htdocs\site.com\image> copy test.png E:\xampp\htdocs\site.com\thumb


but the above command isn't working in script level execution when it is executed as,

Code: Select all
<?php
exec("E:\xampp\htdocs\site.com\image> copy test.png E:\xampp\htdocs\site.com\thumb");
?>


How to execute windows command in php script level in this case?

exec is enabled on my xampp.

Thanks,
optimisticwalker
 
Posts: 23
Joined: 01. September 2018 21:21
XAMPP version: 7.2.9
Operating System: WIndows

Re: Linux like script level exec command on windows xampp

Postby optimisticwalker » 13. September 2018 15:25

Just mixed up the command with linux bash and included the output of cd command subconsciously in exec function.

Developers who are trying to replace linux command with windows using exec() or shell_exec() function on localhost for testing purpose must use backslash for all directory related commands in their script though forward slash is well recognized by many other PHP functions, methods & classes on localhost.

However, the command for copying file from one directory to another using root relative url like linux on windows shall be,

Code: Select all
<?php
exec("copy E:\xampp\htdocs\site.com\image\test.png  E:\xampp\htdocs\site.com\thumb");
?>


To check the output of exec in their scripts, developer can use two optional arguments like,

Code: Select all
<?php
exec("copy E:\xampp\htdocs\site.com\image\test.png  E:\xampp\htdocs\site.com\thumb", $output_in_an_array, $execution_status);

echo '<pre>';
var_dump($output_in_an_array);
echo '<pre>';
echo '<br>';
echo '<pre>';
var_dump($execution_status);
echo '<pre>';
?>


$output_in_an_array displays the command output in a numeric array whereas $execution_status returns integer as command successful or unsuccessful status. if $execution_status returns int(0) command was executed successfully. If it returns int(1) command was unsuccessful.

More details at: http://php.net/manual/en/function.exec.php

Thanks,
optimisticwalker
 
Posts: 23
Joined: 01. September 2018 21:21
XAMPP version: 7.2.9
Operating System: WIndows


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 53 guests