简体   繁体   English

PHP exec() 并显示输出

[英]PHP exec() and display output

I've already seen a lot of solutions online for my problem but I have to admit that most of them did not work for me.我已经在网上看到了很多针对我的问题的解决方案,但我不得不承认其中大多数对我不起作用。 Either they did just not output anything or even broke the upload function in my program, which actually works fine.他们要么没有输出任何内容,要么甚至破坏了我程序中的上传功能,这实际上工作正常。 I have written a little Script for the RaspberryPi enabling people to upload a hex file via http and have it automatically compiled using avrdude.我为 RaspberryPi 编写了一个小脚本,使人们能够通过 http 上传十六进制文件并使用 avrdude 自动编译它。 Maybe I'm just too stupid to figure it out.可能是我太笨了,想不通。

<?php
$target_dir = "upload/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$all_ok = 1;

    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], "upload/upload.hex")) {
        echo "Die Datei ". basename( $_FILES["fileToUpload"]["name"]). " wurde hochgeladen."."<br>";
        $all_ok=1;
    } else {
        $all_ok == 0;
        echo "Sorry, während dem Upload ist ein Fehler aufgetreten!"."<br>";
    }
    
    if ($all_ok == 1){
        $result = exec("/usr/bin/avrdude -c gpio -p m328p /var/www/upload/upload.hex -Uflash:w:/var/www/upload/upload.hex 2>&1", $output, $return_var);
        $result_array=explode(' ',$result);
        echo "Output: ".$result_array."<br>";
        echo "Exit status: ".$return_var."<br>";
    } else {
        echo "Sorry, während dem Kompilieren ist ein Fehler aufgetreten!";
    }   
?>

This is the current version of my program which sadly doesn't display anything more then这是我的程序的当前版本,遗憾的是它没有显示更多内容

Die Datei blink.hex wurde hochgeladen. Die Dateiblink.hex wurde hochgeladen。

Output: Array输出:数组

Exit status: 0退出状态:0

I think the problem is somewhere near我认为问题出在附近

$result_array=explode(' ',$result);
        echo "Output: ".$result_array."<br>";

This was my latest vain approach, hopefully someone will enlighten me.这是我最新的徒劳方法,希望有人能启发我。

$all_ok == 0;

应该

$all_ok = 0;

exec()执行()

exec( string $command [, array &$output [, int &$return_var ]] ) exec( string $command [, array &$output [, int &$return_var ]] )

It means second parameter is your output array.这意味着第二个参数是您的输出数组。 So do not assign the exec to any variable just use所以不要将 exec 分配给任何变量,只需使用

 exec("/usr/bin/avrdude -c gpio -p m328p /var/www/upload/upload.hex -Uflash:w:/var/www/upload/upload.hex 2>&1", $output, $return_var);

print_r($output);
 $result_array=explode(' ',$output);
        echo "Output: ".$result_array."<br>";
        echo "Exit status: ".$return_var."<br>";

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

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