exec("cd /d c:\xampp\htdocs\youtube");
exec("python main.py > c:\xampp\htdocs\youtube\out.txt");
I have written code in php so that it takes a python program and executes and it saves the result in out.txt but it is not working I did the same thing from cmd it is working where am i going wrong
idjaw
26.8k10 gold badges68 silver badges84 bronze badges
2 Answers 2
You should use either
exec("python c:\xampp\htdocs\youtube\main.py > c:\xampp\htdocs\youtube\out.txt");
or
exec("cd /d c:\xampp\htdocs\youtube && python main.py > c:\xampp\htdocs\youtube\out.txt");
Because cd will not retain its directory over multiple exec() statements.
answered Mar 11, 2016 at 20:03
C.Liddell
1,07210 silver badges20 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
suniponna kumar
i have replaced it but the output of main.py is not saving into the output.txt .how to save the output of .py into txt file using cmd from php
You may try:
exec("cd /d c:\xampp\htdocs\youtube && python main.py > c:\xampp\htdocs\youtube\out.txt");
answered Mar 11, 2016 at 20:01
Reto
1,3531 gold badge19 silver badges33 bronze badges
6 Comments
suniponna kumar
i have replaced it but output of main.py is not writting in to output.txt
Reto
Try
exec("cd /d c:\xampp\htdocs\youtube && python -V > c:\xampp\htdocs\youtube\out.txt"); If this writes the python version into your file, then your main.py file doesn't output anything. Else your OS doesn't find python when called from phpsuniponna kumar
print("hiiiii ponna") this is main.py this is php <?php $code=$_POST["code"]; $filename_code="main1.py"; $out="output.txt"; $file_code=fopen($filename_code,"w+"); fwrite($file_code,$code); fclose($file_code); //exec("chmod 777 $out"); //$out=fopen("output.txt","w+"); exec("cd /d c:\xampp\htdocs\youtube && python -V > c:\xampp\htdocs\youtube\out.txt"); echo file_get_contents("output.txt"); fclose($out); ?> where iam going wrong i have writtten a html by post and it submits from that i am trying to exec the python please help
Reto
You are trying to write to a file "out.txt" and then you are trying to read the content of "output.txt"
suniponna kumar
yes but the writng is not happing into out.txt plz helpme
|
lang-php
cddoesn't mean that nextexecwill be called from target directory.