0
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

1
  • 2
    Doing cd doesn't mean that next exec will be called from target directory. Commented Mar 11, 2016 at 19:57

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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
0

You may try:

exec("cd /d c:\xampp\htdocs\youtube && python main.py > c:\xampp\htdocs\youtube\out.txt");

6 Comments

i have replaced it but output of main.py is not writting in to output.txt
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 php
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
You are trying to write to a file "out.txt" and then you are trying to read the content of "output.txt"
yes but the writng is not happing into out.txt plz helpme
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.