3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * 例外スタックを配列に変換する関数 */ function exception_to_array(Exception $e) { do { $m[] = $e->getMessage(); } while ($e = $e->getPrevious()); return array_reverse($m); } /** * 例外を生成する関数 */ function e($message, $previous = null) { return new RuntimeException($message, 0, $previous); } /** * HTML特殊文字をエスケープする関数 */ function h($str) { return htmlspecialchars($str, ENT_QUOTES, 'UTF-8'); } // 例外変数初期化 $e = null; if (isset($_FILES['upfile']['error']) && is_array($_FILES['upfile']['error'])) { try { // 各ファイルをチェック foreach ($_FILES['upfile']['error'] as $k => $error) { try { // 更に配列がネストしていれば不正とする if (is_array($error)) { throw e("[{$k}] パラメータが不正です", $e); } // $_FILES['upfile']['error'][$k] の値を確認 switch ($error) { case UPLOAD_ERR_OK: // OK break; case UPLOAD_ERR_NO_FILE: // ファイル未選択 continue 2; case UPLOAD_ERR_INI_SIZE: // php.ini定義の最大サイズ超過 case UPLOAD_ERR_FORM_SIZE: // フォーム定義の最大サイズ超過 throw e("[{$k}] ファイルサイズが大きすぎます", $e); default: throw e("[{$k}] その他のエラーが発生しました", $e); } // ここで定義するサイズ上限のオーバーチェック if ($_FILES['upfile']['size'][$k] > 1000000) { throw e("[{$k}] ファイルサイズが大きすぎます", $e); } // move_uploaded_file関数での「実際にアップロードされたファイルかどうか」 // のチェックを今回は行わないので、is_uploaded_file関数でのチェックが必要 if (!is_uploaded_file($_FILES['upfile']['tmp_name'][$k])) { throw e("[{$k}] 不正なファイルです", $e); } // $_FILES['upfile']['mime']の値はブラウザ側で偽装可能なので // MIMEタイプに対応する拡張子を自前で取得する $info = getimagesize($_FILES['upfile']['tmp_name'][$k]); if ($info === false) { throw e("[{$k}] 画像ファイルではありません", $e); } switch ($info['mime']) { case 'image/gif': $mime = $ext = 'gif'; break; case 'image/png': $mime = $ext = 'png'; break; case 'image/jpeg': $mime = 'jpeg'; $ext = 'jpg'; break; default: throw e("[{$k}] 画像形式が未対応です", $e); } // 画像処理に使う関数名を決定する $create = "imagecreatefrom{$mime}"; $output = "image{$mime}"; // 縦横比を維持したまま 120 * 120 以下に収まるサイズを求める if ($info[0] >= $info[1]) { $dst_w = 120; $dst_h = 120 * $info[1] / $info[0]; } else { $dst_w = 120 * $info[0] / $info[1]; $dst_h = 120; } // ファイルデータからSHA-1ハッシュを取ってファイル名を決定する $src = $create($_FILES['upfile']['tmp_name'][$k]); $dst = imagecreatetruecolor($dst_w, $dst_h); imagecopyresampled( $dst, $src, 0, 0, 0, 0, $dst_w, $dst_h, $info[0], $info[1] ); $output( $dst, sprintf('./resized/%s.%s', sha1_file($_FILES['upfile']['tmp_name'][$k]), $ext ) ); // リソースを解放 imagedestroy($src); imagedestroy($dst); // 形式上、正しい処理を行ったが例外としてスローしておく throw e("[{$k}] リサイズして保存しました", $e); } catch (RuntimeException $e) { } } } catch (RuntimeException $e) { } } // ヘッダー送信 header('Content-Type: text/html; charset=utf-8'); ?> <!DOCTYPE html> <html lang="ja"> <head> <title>画像アップロード</title> </head> <body> <?php if ($e): ?> <ul> <?php foreach (exception_to_array($e) as $msg): ?> <li><?=h($msg)?></li> <?php endforeach; ?> </ul> <?php endif; ?> <form enctype="multipart/form-data" method="post" action=""> <fieldset> <legend>画像ファイルを選択(JPEG, GIF, PNGのみ対応)</legend> <ul> <?php for ($i = 0; $i < 10; $i++): ?> <li><input type="file" name="upfile[]"></li> <?php endfor; ?> </ul> <input type="submit" value="送信"> </fieldset> </form> </body> </html>
Output for 5.0.0 - 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.30, 7.0.0 - 7.0.20, 7.1.0 - 7.1.33, 7.2.6 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
<!DOCTYPE html> <html lang="ja"> <head> <title>画像アップロード</title> </head> <body> <form enctype="multipart/form-data" method="post" action=""> <fieldset> <legend>画像ファイルを選択(JPEG, GIF, PNGのみ対応)</legend> <ul> <li><input type="file" name="upfile[]"></li> <li><input type="file" name="upfile[]"></li> <li><input type="file" name="upfile[]"></li> <li><input type="file" name="upfile[]"></li> <li><input type="file" name="upfile[]"></li> <li><input type="file" name="upfile[]"></li> <li><input type="file" name="upfile[]"></li> <li><input type="file" name="upfile[]"></li> <li><input type="file" name="upfile[]"></li> <li><input type="file" name="upfile[]"></li> </ul> <input type="submit" value="送信"> </fieldset> </form> </body> </html>
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting ')' in /in/O7lQF on line 6
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting ')' in /in/O7lQF on line 6
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `')'' in /in/O7lQF on line 6
Process exited with code 255.

preferences:
253.2 ms | 401 KiB | 399 Q