0

I need to execute a python script that writes a file in the same directory.

test.py :

print 'Hi! I was executed'

test2.py :

filename = 'sample.txt'
target = open(filename,'a')
target.write("Something Something")
target.close()

Php script:

<?
    exec('python test.py',$output1,$ret1);
    exec('python test2.py',$output2,$ret2);
?>

The first exec works fine but the second script does not, the return var $ret2 is 1. Both the commands work perfectly fine in the terminal. I guess it is a permission issue as php scripts are executed as 'nobody'.

Thanks in advance

4
  • 1
    try to give full path for filename = 'sample.txt' Commented Mar 6, 2014 at 7:05
  • like filename = '/home/pranjal/sample.txt' Commented Mar 6, 2014 at 7:05
  • Ok I will add this as an answer then Commented Mar 6, 2014 at 7:13
  • @SajithNair I have another question here stackoverflow.com/questions/22230525/… Commented Mar 7, 2014 at 5:45

2 Answers 2

0

try to give full path for filename = 'sample.txt'

For e.g.

filename = '/home/pranjal/sample.txt'
Sign up to request clarification or add additional context in comments.

Comments

0

I would consider using a COM:

<?php
$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run("[Your command here]", 0, false); 
?>

Mind you this will not work wonders running in a Linux server.

Also please consider that you are allowing your script to execute code, please make sure it's all safe when it needs to be :)

escapeshellarg()
escapeshellcmd() 

Will remove the commands that you don't want to run.

Comments

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.