0

I run test.php on apache by xampp, exec() is not working, no error and no respond, test.py is not getting executed. please help , Thank you. I have

test.php

exec("C:\Users\Json\AppData\Local\Microsoft\WindowsApps\python.exe C:\xampp\htdocs\app\test.py 2>&1", $output);
print_r($output)

test.py

import os
os.system("start cmd /k")
ColdIV
  • 1,118
  • 2
  • 13
  • 21
Json Prime
  • 180
  • 1
  • 10
  • 2
    Im not really sure whats the deal here, but your are starting an `cmd` (interactive shell) in your `py` script? What should it return, if it normaly opens an cmd window? – Foobar Nov 17 '22 at 09:11
  • So you want a cmd window to appear? That's not so easy. See: https://stackoverflow.com/a/34526406 – tobifasc Nov 17 '22 at 09:12
  • @Foobar no error from the $output , test.py also not run yes i want to start cmd in py from php – Json Prime Nov 17 '22 at 09:15
  • @tobifasc even tho i try to create a txt file with open() in py , it also not working from php exec() – Json Prime Nov 17 '22 at 09:17
  • @JsonPrime Ok, then lookup what tobifasc noted. I never done this in that way. But why not just try to start cmd directly with `exec()`, maybe its easier. But im really dont know. :-) – Foobar Nov 17 '22 at 09:18
  • @Foobar it doesnt matter whats the statement in py file , i also try to create file in py , it also doesnt run from php exec() – Json Prime Nov 17 '22 at 09:20
  • https://stackoverflow.com/questions/26908929/how-to-open-a-new-cmd-window-using-php – Foobar Nov 17 '22 at 09:21
  • 2
    @JsonPrime I have looked a little bit around on stackoverflow and there are mayn questions on that topic. Maybe you should lookup some of them and may find your answer. – Foobar Nov 17 '22 at 09:25

1 Answers1

0

Backslashes will have to be escaped within the path.

So you will have to write it like this:

exec("C:\\Users\\Json\\AppData\\Local\\Microsoft\\WindowsApps\\python.exe C:\\xampp\\htdocs\\app\\test.py 2>&1", $output);
print_r($output);
ColdIV
  • 1,118
  • 2
  • 13
  • 21
  • Array ( [0] => The system cannot execute the specified program. ) – Json Prime Nov 17 '22 at 09:38
  • That sounds like a different problem. Can you run the command within `exec` from the cmd? – ColdIV Nov 17 '22 at 09:54
  • with single slash it runs no error , also py was not running – Json Prime Nov 17 '22 at 09:57
  • It seems like something is wrong with your permissions / python installation. The code works fine for me with the exact paths you are using. – ColdIV Nov 17 '22 at 10:10
  • are you using xampp? – Json Prime Nov 17 '22 at 13:01
  • Yes, XAMPP v3.3.0 with PHP 8.0.8. You could try to run programs from different directories or run XAMPP as admin if you didn't already. But to me it seems that your initial problem is solved and you've encountered a new one. – ColdIV Nov 17 '22 at 13:12
  • Hi , I found what causing the problem, if i install Apache on service , it wont run. Otherwise , it work, any idea? – Json Prime Nov 17 '22 at 13:15
  • What do you mean by "install Apache on service"? You are running apache on XAMPP, aren't you? – ColdIV Nov 17 '22 at 13:21
  • yes running from apache , also installed on service. windows+R , and type in ```service.msc``` you will see apache on the service if you have install apache to service – Json Prime Nov 17 '22 at 13:25
  • left hand side on the xampp control panel, you can see modules service , there has a list of box where you tick to install module to the service – Json Prime Nov 17 '22 at 13:27
  • 1
    Ah, I see. I would assume your apache service doesn't have the correct permissions. You could try to configure the correct user or run it as admin. To do that just double click it in service.msc and there should be an option to login with a specific account. You could also open the path of the *.exe file and configure it to always run as admin. – ColdIV Nov 17 '22 at 13:43
  • The reason i install on service because i wanted to start Apache automatically on startup and start without xampp – Json Prime Nov 17 '22 at 14:00
  • Now i go plan B ,i put xampp.exe to startup folder, and run Apache automatically from xampp minimize, it work – Json Prime Nov 17 '22 at 14:02
  • Thanks for your help, i will try your method if have problem with xampp in the future – Json Prime Nov 17 '22 at 14:03