今天试了下windows下用php调用python的程序。
环境:win10,python3+,Wampserver32
$str = shell_exec ("C:/Users/***/Anaconda3/python C:/wamp/www/***/deal_document.py ".$newfile);
python的路径和py文件的路径要写全,不然会因为python没加入环境变量而无法运行。代码中的$newfile是出入的参数。在python文件deal_document.py中用sys.argv来接收。
import sys
#接收传入的参数
filename = sys.argv[1]
希望php和python大神不要打我这种混用的,哈哈哈。
另外实现了一个用php下载文件的功能,是copy网上的。
当中的精华应该是ob_clean()函数的使用,清空缓存,没有这个函数下载的文件会报错。具体介绍,自行百度。
<meta charset="utf-8">
<?php
$file = './uploads/'.$_GET['name'];
echo $file;
echo filesize($file);
ob_clean();
header("Content-type: application/octet-stream");
header("Accept-Ranges: bytes");
header("content-disposition: attachment; filename=".$file);
header('content-length:'.filesize($file));
readfile($file);
?>