繁体   English   中英

php exec写入文件的python脚本

[英]php exec python script that writes file

我需要执行在同一目录中写入文件的python脚本。

test.py:

print 'Hi! I was executed'

test2.py:

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

PHP脚本:

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

第一个exec可以正常运行,但是第二个脚本则不能,返回var $ ret2为1。这两个命令在终端中都可以正常运行。 我猜这是一个权限问题,因为php脚本以“ nobody”身份执行。

提前致谢

尝试提供filename = 'sample.txt'完整路径

例如

filename = '/home/pranjal/sample.txt'

我会考虑使用COM:

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

请注意,这不会在Linux服务器上运行的奇迹起作用。

另外请考虑您允许脚本执行代码,请确保在需要时所有脚本都是安全的:)

escapeshellarg()
escapeshellcmd() 

将删除您不想运行的命令。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM