我将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))`
执行此操作的最佳方法是将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))