简体   繁体   English

使用exec()在PHP中运行python脚本

[英]Using exec() to run python script in PHP

I want the user to upload a file and then a php file checks if it is valid or not. 我希望用户上传一个文件,然后一个php文件检查它是否有效。 My problem is in running the script_template.py , i used the function exec() to run in command line but it does not return something and i don't understand why. 我的问题是在运行script_template.py时,我使用了函数exec()在命令行中运行,但它不返回任何内容,我也不明白为什么。 Thanks in advance. 提前致谢。 I am using xampp. 我正在使用xampp。

index.html index.html

<form id="upload" method="post" action="upload.php" enctype="multipart/form-data">
            <div id="drop">
                Drop Here

                <a>Browse</a>
                <input type="file" name="upl" multiple />
            </div>

            <input type="submit" value="Upload Image" name="submit">
        </form>

php file php文件

$allowed = array('png', 'jpg', 'gif','txt','xlsx','xls');


if(isset($_FILES['upl']) && $_FILES['upl']['error'] == 0){


    $extension = pathinfo($_FILES['upl']['name'], PATHINFO_EXTENSION);

    if(!in_array(strtolower($extension), $allowed)){
        echo '{"status":"error"}';
        echo ' <p>The uploaded file extension is incorrect, please try again with the correct file </p>'; 
        exit;
    }


 $x=file_validation ( $_FILES['upl']['tmp_name'] )  ;
echo $x;


    if(move_uploaded_file($_FILES['upl']['tmp_name'], 'uploads/'.$_FILES['upl']['name'])){
        echo '{"status":"success"}';
        exit;
    }
}

echo '{"status":"error"}';
echo 'erro';
exit;



function file_validation($fich) {
    $validation= exec('python script_template.py '.$fich) ;
    echo $validation ;
    }

python file python文件

#!C:\Python27\python.exe
print "Content-Type: text/html\n"

import sys
fich_input= sys.argv[1]  
print fich_input


def header(fich):
    """
    Extracts the header and it's values from tab separated file
    Requires: fich is tab separated file
    Ensures: dic and aux are dictionaries, where aux associates each header to it's order
    and dic associates each header value to the order number.
    """
    input_fil=open(fich,'r')
    contador_linha=0
    lines =input_fil.readlines()
    dic={}
    aux={}
    for line in lines:

        contador_linha+=1
        line_words=line.split('\t')
        if contador_linha==1: #header
            aux_counter=1
            header_len=len(line_words)
            for elem in line_words:
                if elem != '\n':
                    aux[aux_counter]=elem
                    aux_counter+=1


        elif contador_linha==2:#create values for keys in header
            aux_counter=1
            for elem2 in line_words:

                if elem2 != '\n':

                    dic[aux_counter]=[elem2]

                    aux_counter+=1

    return (aux,dic)



def header_value(dic1,dic2):
    """
    joins header and it's value
    Requires: dic1 is aux from header(fich) and dic2 is dic from header(fich)
    Ensures: final_dic is a dictionary which associates eache header to value
    """
    final_dic={}
    header_len=len(dic1.keys())
    for number in range(1,header_len+1):


        final_dic[dic1[number]]=dic2[number]
    return final_dic



def mol_name(final_dic):
    """
    Finds molecule name for just labelling
    Requires: final_dic is a dictionary returned by header_value
    Ensures: string representing molecule name
    """
    return final_dic['Mol_name'] 

def print_info(final_dic):
    """
    prints in the screen all information contained in the file given by researcher
    Requires: final_dic from header_value
    Ensures: information in the screen
    """
    print str(mol_name(final_dic))
    for key in final_dic:
        print key,':',final_dic[key]

    print '\n'



def general_operation(fich):
    """
    Gathers information required to start database operations and if not give an error
    Requires: fich is a Tab separated file, dic12 is 
    Ensures: Crucial information to database operations
    """
    dics=header(fich)

    header_values= header_value(dics[0],dics[1])

    type_file=''
    if len(header_values) < 21 :
           type_file='no_ref'
    else:
           type_file='ref'


    return (type_file,header_values)


print 'yes'
print general_operation(fich_input)       

The python script is running just fine, php simply ignores it. python脚本运行得很好,php只是忽略了它。

(I'm assuming you're using Windows.) (我假设您使用的是Windows。)

I think you need to call Python differently. 我认为您需要以不同的方式调用Python。 If you run 如果你跑

python http://localhost/bioinformatica/processa_tsv/AJAX_upload/script_template.py

from the command line on your computer, does it work? 从您计算机上的命令行,行得通吗? I'm guessing not, because the first argument to python should be the name of a Python file on the local drive . 我猜不是,因为python的第一个参数应该是本地驱动器上的 Python文件名。

Try running something like python C:\\path\\to\\script.py to test the command line you'll put in exec() . 尝试运行类似python C:\\path\\to\\script.py以测试将放入exec()的命令行。

Solved it, just by editing this part of the code to: 只需将代码的这一部分编辑为:

function file_validation($fich) {
 $validation=""
 exec('python script_template.py '.$fich,$validation) ;
    return $validation ;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM