PHP + Shell 返回 HTML

标签 php python html linux shell

我目前遇到一个问题,我正在尝试执行解释文件并回显 html 的 shell 脚本,shell 脚本在本地完美执行,但是当我尝试通过 php 执行时,它不会返回任何内容,并且似乎尽管 php 根本不执行它,但我有一个在执行脚本时创建的文件,该文件在 php 执行时不会显示。我有其他使用 php 执行的脚本并且工作完全正常,并且我有适当的 www-data 权限来执行此文件。请帮助我,这是我尝试执行的脚本以及正在执行它的 php 文件。

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<table width="1000" border="0" align="center" cellpadding="0" cellspacing="0">
  <tbody>
    <tr>
      <th style="font-size: 24px" scope="col"><p align="left" class="title_Font">Magnum Dongs <span style="font-size: 18px">&nbsp;|&nbsp; Mighty Meat Sticks 4 Kidz</span></p></th>
    </tr>
  </tbody>
</table>
<ul align="center" width="1000" height="50">
  <li><a href="#home">Home</a></li>
  <li><a href="#news">Stories</a></li>
  <li><a href="#contact">Memes</a></li>
  <li><a href="#about">Shop</a></li>
  <li><a href="#email">Email</a></li>
</ul>
<table width="1000" height="386" border="0" align="center" cellpadding="0" cellspacing="0" class="body_Main">
  <tbody>
    <?php 
    echo exec('./var/www/html/newdongs/fetch_html.sh');
    ?>
  </tbody>
</table>
</body>
</html>

#!/usr/bin/env bash
python3 /bin/dongs/db_Read.py >> lines
#1
echo "<tr>"
echo "<th scope='col'>"
echo "table width='1000' border='0' class='post' align='center' style='margin-top: 10px;'>"
echo "<tbody>"
echo "<tr>"
echo "<th scope='col' width='50'>"
echo "<p style='font-size: 0px; margin:0px; ' align='center'> Posted By: "`awk 'NR==11' lines`"</p>"
echo "<p style='font-size: 0px; margin: 0px; ' align='center'> Total Score: "`awk 'NR==41' lines`"</p>"
echo "<img src='"`awk 'NR==31' lines`"'  width='150' height='150' alt=''/>"
echo "<p style='font-size: 0px; margin: 0px; ' align='center'> Upvote | Downvote</p>"
echo "</th>"
echo "<th scope='col' align='left' valign='top'>"
echo "<p class='post' style='font-size: 14px'>"`awk 'NR==21' lines`"</p>"
echo "</tr>"
echo "</tbody>"
echo "</table></th>"
#2
echo "<tr>"
echo "<th scope='col'>"
echo "table width='1000' border='0' class='post' align='center' style='margin-top: 10px;'>"
echo "<tbody>"
echo "<tr>"
echo "<th scope='col' width='50'>"
echo "<p style='font-size: 0px; margin:0px; ' align='center'> Posted By: "`awk 'NR==12' lines`"</p>"
echo "<p style='font-size: 0px; margin: 0px; ' align='center'> Total Score: "`awk 'NR==42' lines`"</p>"
echo "<img src='"`awk 'NR==32' lines`"'  width='150' height='150' alt=''/>"
echo "<p style='font-size: 0px; margin: 0px; ' align='center'> Upvote | Downvote</p>"
echo "</th>"
echo "<th scope='col' align='left' valign='top'>"
echo "<p class='post' style='font-size: 14px'>"`awk 'NR==22' lines`"</p>"
echo "</tr>"
echo "</tbody>"
echo "</table></th>"
#3
echo "<tr>"
echo "<th scope='col'>"
echo "table width='1000' border='0' class='post' align='center' style='margin-top: 10px;'>"
echo "<tbody>"
echo "<tr>"
echo "<th scope='col' width='50'>"
echo "<p style='font-size: 0px; margin:0px; ' align='center'> Posted By: "`awk 'NR==13' lines`"</p>"
echo "<p style='font-size: 0px; margin: 0px; ' align='center'> Total Score: "`awk 'NR==43' lines`"</p>"
echo "<img src='"`awk 'NR==33' lines`"'  width='150' height='150' alt=''/>"
echo "<p style='font-size: 0px; margin: 0px; ' align='center'> Upvote | Downvote</p>"
echo "</th>"
echo "<th scope='col' align='left' valign='top'>"
echo "<p class='post' style='font-size: 14px'>"`awk 'NR==23' lines`"</p>"
echo "</tr>"
echo "</tbody>"
echo "</table></th>"
#4
echo "<tr>"
echo "<th scope='col'>"
echo "table width='1000' border='0' class='post' align='center' style='margin-top: 10px;'>"
echo "<tbody>"
echo "<tr>"
echo "<th scope='col' width='50'>"
echo "<p style='font-size: 0px; margin:0px; ' align='center'> Posted By: "`awk 'NR==14' lines`"</p>"
echo "<p style='font-size: 0px; margin: 0px; ' align='center'> Total Score: "`awk 'NR==44' lines`"</p>"
echo "<img src='"`awk 'NR==34' lines`"'  width='150' height='150' alt=''/>"
echo "<p style='font-size: 0px; margin: 0px; ' align='center'> Upvote | Downvote</p>"
echo "</th>"
echo "<th scope='col' align='left' valign='top'>"
echo "<p class='post' style='font-size: 14px'>"`awk 'NR==24' lines`"</p>"
echo "</tr>"
echo "</tbody>"
echo "</table></th>"
#5
echo "<tr>"
echo "<th scope='col'>"
echo "table width='1000' border='0' class='post' align='center' style='margin-top: 10px;'>"
echo "<tbody>"
echo "<tr>"
echo "<th scope='col' width='50'>"
echo "<p style='font-size: 0px; margin:0px; ' align='center'> Posted By: "`awk 'NR==15' lines`"</p>"
echo "<p style='font-size: 0px; margin: 0px; ' align='center'> Total Score: "`awk 'NR==45' lines`"</p>"
echo "<img src='"`awk 'NR==35' lines`"'  width='150' height='150' alt=''/>"
echo "<p style='font-size: 0px; margin: 0px; ' align='center'> Upvote | Downvote</p>"
echo "</th>"
echo "<th scope='col' align='left' valign='top'>"
echo "<p class='post' style='font-size: 14px'>"`awk 'NR==25' lines`"</p>"
echo "</tr>"
echo "</tbody>"
echo "</table></th>"
#6
echo "<tr>"
echo "<th scope='col'>"
echo "table width='1000' border='0' class='post' align='center' style='margin-top: 10px;'>"
echo "<tbody>"
echo "<tr>"
echo "<th scope='col' width='50'>"
echo "<p style='font-size: 0px; margin:0px; ' align='center'> Posted By: "`awk 'NR==16' lines`"</p>"
echo "<p style='font-size: 0px; margin: 0px; ' align='center'> Total Score: "`awk 'NR==46' lines`"</p>"
echo "<img src='"`awk 'NR==36' lines`"'  width='150' height='150' alt=''/>"
echo "<p style='font-size: 0px; margin: 0px; ' align='center'> Upvote | Downvote</p>"
echo "</th>"
echo "<th scope='col' align='left' valign='top'>"
echo "<p class='post' style='font-size: 14px'>"`awk 'NR==26' lines`"</p>"
echo "</tr>"
echo "</tbody>"
echo "</table></th>"
#7
echo "<tr>"
echo "<th scope='col'>"
echo "table width='1000' border='0' class='post' align='center' style='margin-top: 10px;'>"
echo "<tbody>"
echo "<tr>"
echo "<th scope='col' width='50'>"
echo "<p style='font-size: 0px; margin:0px; ' align='center'> Posted By: "`awk 'NR==17' lines`"</p>"
echo "<p style='font-size: 0px; margin: 0px; ' align='center'> Total Score: "`awk 'NR==47' lines`"</p>"
echo "<img src='"`awk 'NR==37' lines`"'  width='150' height='150' alt=''/>"
echo "<p style='font-size: 0px; margin: 0px; ' align='center'> Upvote | Downvote</p>"
echo "</th>"
echo "<th scope='col' align='left' valign='top'>"
echo "<p class='post' style='font-size: 14px'>"`awk 'NR==27' lines`"</p>"
echo "</tr>"
echo "</tbody>"
echo "</table></th>"
#8
echo "<tr>"
echo "<th scope='col'>"
echo "table width='1000' border='0' class='post' align='center' style='margin-top: 10px;'>"
echo "<tbody>"
echo "<tr>"
echo "<th scope='col' width='50'>"
echo "<p style='font-size: 0px; margin:0px; ' align='center'> Posted By: "`awk 'NR==18' lines`"</p>"
echo "<p style='font-size: 0px; margin: 0px; ' align='center'> Total Score: "`awk 'NR==48' lines`"</p>"
echo "<img src='"`awk 'NR==38' lines`"'  width='150' height='150' alt=''/>"
echo "<p style='font-size: 0px; margin: 0px; ' align='center'> Upvote | Downvote</p>"
echo "</th>"
echo "<th scope='col' align='left' valign='top'>"
echo "<p class='post' style='font-size: 14px'>"`awk 'NR==28' lines`"</p>"
echo "</tr>"
echo "</tbody>"
echo "</table></th>"
#9
echo "<tr>"
echo "<th scope='col'>"
echo "table width='1000' border='0' class='post' align='center' style='margin-top: 10px;'>"
echo "<tbody>"
echo "<tr>"
echo "<th scope='col' width='50'>"
echo "<p style='font-size: 0px; margin:0px; ' align='center'> Posted By: "`awk 'NR==19' lines`"</p>"
echo "<p style='font-size: 0px; margin: 0px; ' align='center'> Total Score: "`awk 'NR==49' lines`"</p>"
echo "<img src='"`awk 'NR==39' lines`"'  width='150' height='150' alt=''/>"
echo "<p style='font-size: 0px; margin: 0px; ' align='center'> Upvote | Downvote</p>"
echo "</th>"
echo "<th scope='col' align='left' valign='top'>"
echo "<p class='post' style='font-size: 14px'>"`awk 'NR==29' lines`"</p>"
echo "</tr>"
echo "</tbody>"
echo "</table></th>"
#10
echo "<tr>"
echo "<th scope='col'>"
echo "table width='1000' border='0' class='post' align='center' style='margin-top: 10px;'>"
echo "<tbody>"
echo "<tr>"
echo "<th scope='col' width='50'>"
echo "<p style='font-size: 0px; margin:0px; ' align='center'> Posted By: "`awk 'NR==20' lines`"</p>"
echo "<p style='font-size: 0px; margin: 0px; ' align='center'> Total Score: "`awk 'NR==50' lines`"</p>"
echo "<img src='"`awk 'NR==40' lines`"'  width='150' height='150' alt=''/>"
echo "<p style='font-size: 0px; margin: 0px; ' align='center'> Upvote | Downvote</p>"
echo "</th>"
echo "<th scope='col' align='left' valign='top'>"
echo "<p class='post' style='font-size: 14px'>"`awk 'NR==30' lines`"</p>"
echo "</tr>"
echo "</tbody>"
echo "</table></th>"

感谢您提前提供的帮助!

最佳答案

我认为问题在于 PHP 在错误的位置寻找脚本。

您可以使用 getcwd() 获取当前工作目录。检查您是否使用当前工作目录中脚本的正确相对路径。

否则您可以简单地将脚本的绝对路径传递给 exec()命令。

下一个问题是您的 shell 脚本打印了相当多的行并以标签 </table></th> 结尾。 。 exec()函数仅返回 shell 脚本输出的最后一行。因此 PHP 输出将仅显示 </table></th> 。这不能以 HTML 形式呈现,因为它们只是结束标记。因此,您在浏览器中看不到任何变化,但如果检查 HTML,您可以注意到它。

您可以使用 shell_exec()返回 shell 脚本的整个输出。

这是更正后的版本:

<?php 
// 1.Remove the leading dot. The leading dot makes the path relative.
// 2.Use shell_exec instead to get all lines from your script output.
echo shell_exec('/var/www/html/newdongs/fetch_html.sh'); 
?>

关于PHP + Shell 返回 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37130281/

相关文章:

php-mysql : different access page during login

php - 如何检查 PHP 哈希表中是否存在键?

python - 删除 HTML 文件中的换行符

javascript - 替换 observableArray?

php - 一张表的不同表前缀?

php - Laravel 5 和 Quickbooks

python - 正则表达式包含 "times"但不包含 "clock"

python - 在 Dask 中高效删除行

html - CSS::before 伪元素行高?

html - 如何使用 Bootstrap 使图像响应