Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
上载文档后在PHP中运行bash脚本_Php_Bash - Fatal编程技术网

上载文档后在PHP中运行bash脚本

上载文档后在PHP中运行bash脚本,php,bash,Php,Bash,我需要一些指导,为这个项目我的工作。 我有一个处理数据的脚本,我想在服务器上使用它 如何集成命令以运行位于ProcessedData/please的script.sh 多谢各位 <?php if(isset($_FILES['file'])){ $errors= array(); $file_name = $_FILES['file']['name']; $file_size =$_FILES['file']['size']; $fil

我需要一些指导,为这个项目我的工作。 我有一个处理数据的脚本,我想在服务器上使用它

如何集成命令以运行位于ProcessedData/please的script.sh

多谢各位

<?php
   if(isset($_FILES['file'])){
      $errors= array();
      $file_name = $_FILES['file']['name'];
      $file_size =$_FILES['file']['size'];
      $file_tmp =$_FILES['file']['tmp_name'];
      $file_type=$_FILES['file']['type'];
      $file_ext=strtolower(end(explode('.',$_FILES['file']['name'])));

  $extensions= array("log");

  if(in_array($file_ext,$extensions)=== false){
     $errors[]="extension not allowed, please choose a log file.";
  }

  if($file_size > 2097152){
     $errors[]='File size must be excately 2 MB';
  }

  if(empty($errors)==true){
     move_uploaded_file($file_tmp,"ProcessedData/".$file_name);
     echo "Success";
  }else{
     print_r($errors);
  }
   }
exec(parsing.sh);
?>
您可以使用execscriptName执行任何脚本。

这里是解决方案

<?php
   if(isset($_FILES['file'])){
      $errors= array();
      $file_name = $_FILES['file']['name'];
      $file_size =$_FILES['file']['size'];
      $file_tmp =$_FILES['file']['tmp_name'];
      $file_type=$_FILES['file']['type'];
      $file_ext=strtolower(end(explode('.',$_FILES['file']['name'])));

  $extensions= array("log");

  if(in_array($file_ext,$extensions)=== false){
 $errors[]="extension not allowed, please choose a log file.";
  }

  if($file_size > 2097152){
 $errors[]='File size must be excately 2 MB';
  }

  if(empty($errors)==true){
     move_uploaded_file($file_tmp,"ProcessedData/".$file_name);
     echo "Success";
  }else{
     print_r($errors);
     }
       }
echo shell_exec('sh parsing.sh');
?>

嗨,谢谢你的回答,我试过了,但什么都没有发生,到目前为止我没有任何错误消息。答案有点简单。您可以通过谈论转义shell参数来改进这个答案。例如,我在您的代码中没有看到任何exec调用。。。既然我们在讨论代码,我们能看到它吗?@Mohamed只要你的目录被正确访问,请为脚本提供正确的位置,尝试使用绝对路径,这必须有效。@electric.Kariim我已经编辑了我的代码,但仍然没有发生任何事情。我的脚本与我的php文件位于同一位置,我有一个名为ProcessedData的文件夹,我的文件上传到这个文件夹。我的编辑有什么问题吗?parsing.sh是字符串吗?这是无效的语法,因为。。。execparsing.sh;另外,为了帮助测试您的代码,我们希望看到exec的返回值$retval=exec$scriptName;是的,从shell调用执行就像。。。贝壳/@ficuscr,还有其他选择吗?换一种说法,如果在命令行/提示符中键入它时不起作用,那么在exec中也不起作用。所以是的,显然是sh script.sh或./script.sh。与PHP或您最初要求的任何内容几乎没有关系。我会结束这一切,因为它不太可能帮助其他人。@ficuscr我明白你的意思,请你用PHP推荐什么?除了小心之外,我没有其他要补充的。
#!/bin/bash

grep -w BLABLABLA ProcessedData/ant_traps.log > result.csv
<?php
   if(isset($_FILES['file'])){
      $errors= array();
      $file_name = $_FILES['file']['name'];
      $file_size =$_FILES['file']['size'];
      $file_tmp =$_FILES['file']['tmp_name'];
      $file_type=$_FILES['file']['type'];
      $file_ext=strtolower(end(explode('.',$_FILES['file']['name'])));

  $extensions= array("log");

  if(in_array($file_ext,$extensions)=== false){
 $errors[]="extension not allowed, please choose a log file.";
  }

  if($file_size > 2097152){
 $errors[]='File size must be excately 2 MB';
  }

  if(empty($errors)==true){
     move_uploaded_file($file_tmp,"ProcessedData/".$file_name);
     echo "Success";
  }else{
     print_r($errors);
     }
       }
echo shell_exec('sh parsing.sh');
?>