teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

修正

2019/12/22 06:57

投稿

mpyw
mpyw

スコア5223

answer CHANGED
@@ -22,7 +22,15 @@
22
22
  ]));
23
23
 
24
24
  exec($cmd, $output, $status);
25
+ $output = implode("\n", $output) . "\n";
25
26
 
27
+ // htmlspecialchars や nl2br での変換を省略するために text/html ではなく text/plain を使う
26
- header('Content-Type: text/html; charset=UTF-8');
28
+ header('Content-Type: text/plain; charset=UTF-8');
29
+
30
+ if ($status === 0) {
31
+ echo $output;
32
+ } else {
33
+ http_response_code(500);
27
- echo nl2br(implode("\n", $output) . "\n", false);
34
+ echo "Command failed with status $status: $output";
35
+ }
28
36
  ```

1

更新

2019/12/22 06:57

投稿

mpyw
mpyw

スコア5223

answer CHANGED
@@ -1,2 +1,28 @@
1
1
  `python` にパスが通っていないとか…?
2
- `python.exe` のある場所を完全に指定してみてください。
2
+ `python.exe` のある場所を完全に指定してみてください。
3
+
4
+ ----
5
+
6
+ 追記
7
+
8
+ ```python
9
+ # -*- coding: utf-8 -*-
10
+
11
+ print("a")
12
+ print("b")
13
+ print("c")
14
+ ```
15
+
16
+ ```php
17
+ <?php
18
+
19
+ $cmd = implode(' ', array_map('escapeshellarg', [
20
+ 'C:\python\python.exe',
21
+ 'C:\xampp\test2.py', // ← htdocs の中に入れずに直接アクセスを防ぐほうがおすすめです
22
+ ]));
23
+
24
+ exec($cmd, $output, $status);
25
+
26
+ header('Content-Type: text/html; charset=UTF-8');
27
+ echo nl2br(implode("\n", $output) . "\n", false);
28
+ ```