php exec 写文件权限,写入文件的php exec python脚本(php exec python script that writes file)...

写入文件的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'。

提前致谢

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

原文:https://stackoverflow.com/questions/22217403

更新时间:2020-01-30 18:26

最满意答案

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

例如

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

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

For e.g.

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

2014-03-06

相关问答

exec()命令不直接计入PHP,因为该过程是单独执行的,而不是从PHP进程内部执行的。 话虽如此,如果您使用任何输出变量与命令,他们将计入限制。 因此,如果您有一个产生大量详细日志记录的文件,并且您正在捕获该日志记录,那么它将计入内存限制。 The exec() command does not directly count towards the PHP since the process is executed separately not from within the PHP Proce

...

这部分 $("#id").html(data);

表示jQuery使用您的Python脚本的响应填充id为“id”的元素。 问题是你没有id="id"的元素。 你还想做的是把$("#img").hide(); 在$.post成功处理程序中。 这样, $.post完成后隐藏图像。 使用当前代码,它会立即隐藏,因为$.post是一个异步请求,因此任何其他代码都不会等待它。 您的代码应如下所示:

loader.gif

...

我找到了一个解决方案。 问题出在cron脚本上。 如果我将参数'-O'添加到wget命令,则不会获得空文件。 wget -O - https://[lanAddress]/path/To/Script >> /dev/null 2>&1

I have found a solution. The problem was with the cron script. If I add the argument '-O' to the wget command I get no empty file.

...

尝试为filename = 'sample.txt'提供完整路径 例如 filename = '/home/pranjal/sample.txt'

try to give full path for filename = 'sample.txt' For e.g. filename = '/home/pranjal/sample.txt'

这是超时工作wrt exec()的方式。 所以exec()方法的第一行是这样的: $this->curTimeout = $this->timeout;

后来有一个while (true)循环,其中包含以下行: $temp = $this->_get_channel_packet(self::CHANNEL_EXEC);

_get_channel_packet也有一个while (true)循环。 它会循环,直到它超时或直到它在适当的通道上接收数据。 这是超时代码: if ($

...

点击我的头后,我试着改变bash脚本上的Python脚本的路径,然后添加/ root /并且它现在正在工作。 Ok after a click on my head, I tried to change the path to the Python script on the bash script and voilá, just added /root/ and it's working now.

你尝试将$ output写为字符串,但它是一个数组 使用foreach或$ output [0] You try write $output as string, but it`s an array Use foreach or $output[0]

你有没有使用相对或绝对路径? 尝试使用绝对路径并检查你的工作目录是什么。 我认为你正在启动python脚本而不是你认为你的目录。 :) Thank you to user2693053 for solving this problem: Sounds like a privilege issue, although I am not experienced enough with calling Python from php to know whether that could be an is

...

您是否考虑过使用像JSON这样的序列化数据格式? 它很轻巧,可以轻松转换为Python词典,所有酷孩子都在使用它。 您可以以有意义的方式构造数据,但不需要包含实际代码。 然后,您可以读取该构造,获取所需的部分,然后将其传递给函数或类。 编辑 :在可能的JSON规范的简洁示例中添加了一个传递。 你的JSON: {

"macros": [

{

"function": "foo_func",

"args": {

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值