I m getting an error while passing base64 encoded image String to python Script ....that unable to run shell_execute("python hello.py $data") Here is my code
php File
<?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 File
import sys
import base64
image_64_encode=sys.argv[1]
with open("image.jpg","wb") as fh:
fh.write(base64.decodebytes(image_64_encode))`
The best way to do this is to write your base64 to a separated file so that no encoding is needed .. I have slightly altered your code to reflect this.
PHP FILE
<?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 FILE
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))