【问题标题】:Input from textbox and file in PHP form以 PHP 形式从文本框和文件输入
【发布时间】:2014-02-03 13:03:22
【问题描述】:

我尝试制作一个 html 和 php 页面来分别接受输入和处理它们。在 HTML 页面中,用户可以选择提供文本或上传文件。

<form action="target.php" method='POST' enctype="multipart/form-data"            id="form">
<table  width="950px" align="center">
<tr>
<table width="100%" align="center" cellpadding="0" cellspacing="0"  class="table1"  border="0">
<tr valign="middle" align="left"><td width="15" height="10"></td><td></td></tr>
<tr valign="middle" align="left">
<td width="15"></td>
<td><font  color="White">input1</font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<td width="15"></td>
<td>
<font color="Red"><input type="file" name="file" size="42"><br></font><font color="White">or paste below:</font><br>
<textarea name="sequence1" cols="96" rows="7"></textarea><br>     
</font>&nbsp;&nbsp;&nbsp;&nbsp;
</td>
</tr>
</table> </form>

类似地在 PHP 页面中,我可以成功地将上传的文件从 html 页面传输到变量并运行 shell 脚本,但无法处理来自文本框的内容。

<?php
$a = $_FILES['file']['tmp_name'];
$c = (isset($_POST['sequence1'])) ? $_POST['sequence1'] : false;
if($a!=NULL)
{
$output=shell_exec("sh server.sh $a");
 }
elseif ($c!=NULL){
$output=shell_exec("sh server.sh $c");}
else{
echo "No input";}
?>

任何人都可以帮我解决这个问题,或者您可以帮助我的其他方式将文本输入变量转换为 shell 脚本的文件

【问题讨论】:

  • 至少你的表单标签没有关闭
  • $a 需要对 isset() 进行三元分配。如果不使用'" 将其封装为字符串,则无法回显no input
  • &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 是啊,反正谁需要 CSS。
  • @VladNikitin:我已经给出了表格的一部分,所以它没有出现。我已经以原始形式关闭了它。
  • @Ohgodwhy:我试图给出三元赋值 isset(),但在代码中并没有真正帮助我!!..做了一些小改动

标签: javascript php html shell


【解决方案1】:

如果我理解你的话,这就是你需要做的:

<?php
if(isset($_POST['sequence1'])){
    //get the input content
    $textInput = $_POST['sequence1'];
    //temp file name
    $file = 'temp.txt';
    // Write the contents back to the file
    file_put_contents($file, $textInput);
    $output=shell_exec("sh server.sh $file");
}else{
    echo "No input";
}
?>

【讨论】:

  • 这不行!!..因为我写的shell脚本把文件名作为参数。!!
  • 当我通过 html 表单上传文件时,它们是完美的过程,没有任何问题。但是当我将相同的数据粘贴在表单的文本框中时。它没有给我输出。我理解的最可能的原因是我编写的 shell 脚本将参数作为文件名而不是直接作为文本。实际上,我需要帮助才能将此文本输入存储在临时文件中并提供给 shell 脚本!!.. 我希望这会让你清楚!!
  • 我已经尝试过了,但是 shell 脚本没有接受它。可能需要定义路径。我试图回显 $file,它给出了 temp.txt,这意味着文本在文件中,但 shell 脚本无法找到文件的路径以将其作为输入
  • @RamT 要检查的东西,确保 a) temp.txt 是使用内容创建的 b) 确保 server.sh 和 @ 987654324@ 位于同一目录中服务器
【解决方案2】:

在您的 php 代码中解析错误。

  echo No input;

应该是

echo "No input";

【讨论】:

    猜你喜欢
    • 2014-02-23
    • 2010-12-31
    • 2012-07-08
    • 1970-01-01
    • 1970-01-01
    • 2017-05-07
    • 1970-01-01
    • 2010-11-23
    • 2019-08-03
    相关资源
    最近更新 更多