【问题标题】:Is there any method to pass an Image as a Variable from php to python Script有什么方法可以将图像作为变量从 php 传递到 python 脚本
【发布时间】:2019-04-19 18:33:53
【问题描述】:

我在将 base64 编码的图像字符串传递给 python 脚本时遇到错误......无法运行 shell_execute("python hello.py $data") 这是我的代码

php 文件

 <?php
 echo "Good Work";
 $img = file_get_contents("aaaa.jpg");
 $data = base64_encode($img);
 $output = shell_exec("python hello.py".$data);
 echo $output;
 ?>

hello.py 文件

import sys
import base64
image_64_encode=sys.argv[1]
with open("image.jpg","wb") as fh:
     fh.write(base64.decodebytes(image_64_encode))`

【问题讨论】:

  • 你需要为你的论点留一个空间.. $output = shell_exec("python hello.py".$data); 需要看起来像$output = shell_exec("python hello.py " . $data); 否则 如果data='foo'; 它看起来像shell_exec("python hello.pyfoo");
  • 另外 .. 是否在您的 php 配置中启用了 shell_exec?大多数安装都带有“黑名单”,需要在.ini 中启用
  • 扎克先生,它不起作用
  • 先生,当我传递简单的字符串时,例如 $str="sohaib" 而不是 $data 然后它可以工作,但如果是图像它不起作用我不知道为什么
  • Base64 是否包含会与arg 混淆的内容,例如$"' 等等等等等等..

标签: php python exec


【解决方案1】:

最好的方法是将你的 base64 写入一个单独的文件,这样就不需要编码了。我稍微修改了你的代码以反映这一点。

PHP 文件

<?php
$img = file_get_contents("image.jpg");
$data = base64_encode($img);
$content = $data;
$file = "myFile.base64";
$fp = fopen($file,"wb");
fwrite($fp,$content);
fclose($fp);

$output = shell_exec("python hello.py $file");
echo $output;
?>

Python 文件

import sys
import base64
file=sys.argv[1]
f = open(file)
base_64 = f.read()

# print (base_64)
# Do what you want with the base_64 variable.

image_64_encode=base_64
with open("image.jpg","wb") as fh:
   fh.write(base64.decodebytes(image_64_encode))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-11
    相关资源
    最近更新 更多