Question

I am trying to run the xcopy command from the php via exec() function : the following are my codes

$execute = ('xcopy "\\\\192.1.1.52\\xcopy_folder\\testfile.txt" "c:\\xcopy_folder\\"',$output);
exec($execute,$output);
echo $output."<br />";
echo $execute;

it shows 0 file copied together with the command which was executed, I then copy and paste the command to cmd prompt, it does copy the file to my destination folder, can anyone please assist, thank you !

I have tried the following :

  1. instead of using the computer name, I tried the IP address (does the same thing - php does not work but working with DOS prompt), still shows 0 file copied.

  2. instead of using the computer name and ip address, I mapped a drive(G:) to the folder and use the drive symbol instead (it also not working with exec but works fine with DOS prompt)- same result as above.

  3. the computer contains the source file is locate overseas (slow connection), and I've tried to copy the file that is in the same network (next to my computer), does not matter if I use the IP address or computer name it works like charm.

Please help :(

Was it helpful?

Solution 2

Most of the time, if a command works from the command prompt, but not from PHP's exec() (or shell_exec() command), it is because of one of the following:

1) A permissions problem. You have the permissions necessary to run the command as the user that you are executing the command as from the command line. But, the user that PHP is running as does not have necessary permissions.

2) An environment problem. An environment variable (such as a path) is defined in the environment of the user that you are executing the command as from the command line. But, this environment variable is not setup in the environment of the user that PHP is running as.

OTHER TIPS

Try this bellow

$execute = ('xcopy "\\\\192.1.1.52\\xcopy_folder\\testfile.txt" "c:\\xcopy_folder\\"');
exec($execute,$output);
print_r ($output)."<br />";
echo $execute;

If it doesn't work then check

  1. The files exist
  2. You have permissions
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top