try to give full path for filename = 'sample.txt'
For e.g.
filename = '/home/pranjal/sample.txt'
문제
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
해결책
try to give full path for filename = 'sample.txt'
For e.g.
filename = '/home/pranjal/sample.txt'
다른 팁
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.