回答編集履歴

2

修正

2019/12/22 06:57

投稿

mpyw
mpyw

スコア5223

test CHANGED
@@ -46,10 +46,26 @@
46
46
 
47
47
  exec($cmd, $output, $status);
48
48
 
49
+ $output = implode("\n", $output) . "\n";
49
50
 
50
51
 
51
- header('Content-Type: text/html; charset=UTF-8');
52
52
 
53
+ // htmlspecialchars や nl2br での変換を省略するために text/html ではなく text/plain を使う
54
+
55
+ header('Content-Type: text/plain; charset=UTF-8');
56
+
57
+
58
+
59
+ if ($status === 0) {
60
+
61
+ echo $output;
62
+
63
+ } else {
64
+
65
+ http_response_code(500);
66
+
53
- echo nl2br(implode("\n", $output) . "\n", false);
67
+ echo "Command failed with status $status: $output";
68
+
69
+ }
54
70
 
55
71
  ```

1

更新

2019/12/22 06:57

投稿

mpyw
mpyw

スコア5223

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