如何在php中运行命令行函数
我在php中从命令行运行程序时遇到一些问题。每当我在命令行中运行程序时,都会创建正确的文件,并且它工作得非常好。但是,当我把它放在php中并使用exec()函数时,什么都没有发生 最后,我希望用户能够选择他想要在外部程序上运行的参数,但现在我只是尝试在…中使用硬编码的值来运行它 下面是我用来从命令行运行程序的命令及其参数如何在php中运行命令行函数,php,html,mysql,exec,external,Php,Html,Mysql,Exec,External,我在php中从命令行运行程序时遇到一些问题。每当我在命令行中运行程序时,都会创建正确的文件,并且它工作得非常好。但是,当我把它放在php中并使用exec()函数时,什么都没有发生 最后,我希望用户能够选择他想要在外部程序上运行的参数,但现在我只是尝试在…中使用硬编码的值来运行它 下面是我用来从命令行运行程序的命令及其参数 [path1] -p blastp -d [parameter1] -i [path2] -e [parameter2] -m 9 -o [path3] where [p
[path1] -p blastp -d [parameter1] -i [path2] -e [parameter2] -m 9 -o [path3]
where [path1] is the path to ../blast-2.2.26/bin/blastall,
[path2] is the path to sample.fasta
[path3] is the path for the output file (you may want to create another folder for generating the output)
[parameter1] is the name of database chosen by the user from your page (eg. Human.db, Viruses.db, etc)
[parameter2] is the E-value given by the user from your page(eg. 0.0001, 1, 1000, etc)
我将在命令行中键入的实际代码是
~/blast/blast-2.2.26/bin/blastall -p blastp -d db -i ~/temp/sample.fasta -m 9 -o output'
这是到目前为止我的代码
<form method="POST", action="/~cs4380sp15grp4/home/blast.php">
<select id="database" name="database">
<option selected="selected" disabled="disabled">Database</option>
<option value="Archaea">Archaea</option>
<option value="Bacteria">Bacteria</option>
</select>
<select id="evalue" name="evalue">
<option selected="selected" disabled="disabled"> evalue <option>
<option value="0.0001">0.0001</option>
<option value="0.001">0.001</option>
</select>
<select id="hits" name="hits">
<option selected="selected" disabled="disabled"> Hits</option>
<option value="50">50</option>
</select>
<input id="BlastSearch" type="text" name="BlastSearch" value='' />
<input type="submit" name="submit" value="submit" />
<button type="reset" value="Clear">Clear</button>
</form>
<?php
session_start();
require_once '../secure/database.php';
$mysqli = new mysqli($dbhost,$dbuser,$dbpass,$dbname);
if($mysqli->connect_error){
exit('CON Error: ' . $mysqli->connect_errno . ' ' . $mysqli->connect_error);
}
//Insert the values into the database
if(isset($_POST['submit'])){
$db = $_POST['database'];
$evalue = $_POST['evalue'];
$sequence = $_POST['BlastSearch'];
$mysqli->query("INSERT INTO `Job` (`uid`, `input`, `status`, `start_time`, `finish_time`) VALUES ('1', '" . $sequence . "', 'running' , NOW(), NULL)");
$mysqli->query("INSERT INTO `BLAST`(`database`, `evalue`, 'job_id') VALUES ('" . $db . "','" . $evalue . "', `1`)") or die(mysqli_error($db));
exec(' /students/groups/cs4380sp15grp4/blast/blast-2.2.26/bin/blastall -p blastp -d db -i /students/groups/cs4380sp15grp4/temp/sample.fasta -m 9 -o /students/groups/cs4380sp15grp4/temp/output');
}
// print "Connected! Host info: " . $mysqli->host_info . "<br>\n";
$mysqli->close();
?>
数据库
古菌
细菌
评估
0.0001
0.001
击打
50
清楚的
使用完整路径,即…:
exec(' /full/path/to/blast/blast-2.2.26/bin/blastall -p blastp -d db -i /full/path/to/temp/sample.fasta -m 9 -o output');
}
~
是当前活动用户的主文件夹。Php脚本由用户执行www-data
,主文件夹为/var/www
要解决此问题,请使用完整路径。例如:
exec(' /home/username/blast/blast-2.2.26/bin/blastall -p blastp -d db -i /home/username/temp/sample.fasta -m 9 -o output');
在您的情况下,您可能希望使用您作为谁运行服务器?您以谁的身份从命令行运行代码?什么是~/blast/blast-2.2.26/bin/blastall的权限和所有权?我有正确的权限。虽然命令在PHP内部运行,而PHP又在web服务器内部运行,但需要权限的不是您,而是web服务器。因为这看起来像一个共享的大学系统,所以可能还有其他的安全配置。您是否尝试过运行一个非常简单的命令,只是为了证明exec
能够正常工作?(调试的第一个技巧:分解问题以消除问题。此处发布的代码在exec
行之外有很多东西,可能与问题无关。去掉它!)添加>/var/www/log.txt
,将命令输出重定向到文件。打开该文件以获取有关问题的更多信息。只需重新放置exec
行:exec('/home/username/blast/blast-2.2.26/bin/blastall-p blastp-d db-i/home/username/temp/sample.fasta-m 9-o输出>/var/www/log.txt')代码>(并删除命令前的空格)好的,谢谢,我会尝试一下。这有什么用?我找不到此文件在/var/www/log.txt
中应该有一个文件,其中包含您的命令生成的所有输出。如果没有,您可以尝试使用echo exec(xy)