私の説明はコメントアウトされており、私は説明したようにコードを作成しようとしています。PHPの配列とスイッチの学習
変数をintvalに渡して間違っていると感じ、スイッチメソッドを間違って実装し、nullでないかどうかチェックします。
<?php
// $IMAGE_DIR_PREFIX refers to the provided images subdirectory...
$IMAGE_DIR_PREFIX = 'images/';
// images/globals.php defines two arrays representative of the images
// that can be loaded...
require_once($IMAGE_DIR_PREFIX.'globals.php');
// By default assume no image was selected so:
// a) the image file ($IMAGE) is set to null
// b) the image file's MIME type is set to null...
$IMAGE = null;
$IMAGE_MIME = null;
// pic.php will be accessed using URL parameters, e.g., pic.php?i=2.
// Per what was demonstrated in class obtain the GET parameter's
// key-value pages using the global array $_GET. The parameters are
// as follows:
// a) 'i' is an image number (e.g., 0, 1, 2, 3, etc.)
// b) 'purpose' is either 'g' or 'a'
//
// So:
// * if purpose=g then look up the image file to use in the $LETTERS
// array where i=<INTEGER> is the index into $LETTTERS
// * if purpose=a then look up the image file to use in the $NUMBERS
// array where i=<INTEGER> is the index into $NUMBERS
//
// Sample (partial) URLs:
// * pic.php?i=3&purpose=g
// * pic.php?purpose=a&i=2
//
// Because an invalid index can be passed, before accessing the array
// do the following:
// 1) Obtain the $_GET for 'i', e.g., $_GET['i'].
$_GET['i'];
// 2) Pass (1) into PHP's intval(). This will ensure it is an int.
// NOTE: http://php.net/manual/en/function.intval.php
intval ($i);
// 3) Obtain the $_GET for 'purpose'.
$_GET['purpose'];
// 4) Use a switch statement or a suitable equivalent on (3).
// 4a) If (4) matches 'a' then call array_key_exists() on $NUMBERS
// with the value obtained in (2) as the index. If the key exists
// set $IMAGE to the concatenation of $IMAGE_DIR_PREFIX, '/', and
// the string at index in $NUMBERS.
// 4b) If (4) matches 'g' then call array_key_exists() on $LETTERS
// with the value obtained in (2) as the index. If the key exists
// set $IMAGE to the concatenation of $IMAGE_DIR_PREFIX, '/', and
// the string at index in $LETTERS.
// NOTE: In PHP the '.' operator is string concatenation.
switch (purpose){
case a:
if (array_key_exists('i', $NUMBERS)) {
$IMAGE .= $IMAGE_DIR_PREFIX . '/'. $NUMBERS;
}
case g:
if (array_key_exists('i', $LETTERS)) {
$IMAGE .= $IMAGE_DIR_PREFIX . '/'. $LETTERS;
}
}
// 5) Now that a possible $IMAGE has been set (NOTE: it still might be null),
// determine the image's MIME type. Instead of hard-coding it, you will
// use PHP's finfo object to determine the MIME type of the file.
// Internally the finfo object determines the MIME type using magic strings
// (i.e., in the way the 'file' command does this from the command line).
// Determine the MIME type as follows:
// 5a) If $IMAGE is NOT null:
if (!empty($IMAGE)) {
$fi = new finfo(FILEINFO_MIME);
$result = $fi->file($IMAGE);
if (!empty($result)) {
$IMAGE_MIME=$result;
}
}
// 5b) $fi = new finfo(FILEINFO_MIME);
// $result = $fi->file($IMAGE); // Returns MIME type unless an error occurred
// 5c) If $result is not an error, set $IMAGE_MIME to $result.
// 6) If $IMAGE and $IMAGE_MIME are both NOT null, then output the image with:
// header('Content-Type: '.$IMAGE_MIME);
// @readfile($IMAGE);
// exit;
if (!empty($IMAGE) && !empty($IMAGE_MIME)) {
header('Content-Type: '.$IMAGE_MIME);
@readfile($IMAGE);
exit;
}
// 7) Otherwise, generate an HTTP 404 File Not Found response and page with:
// http_response_code(404);
// echo <<<ZZEOF
// <!DOCTYPE html>
// <html>
// <head><title>2017W 03-60-334 Assignment 2: 404 File Not Found</title></head>
// <body><h1>404 File Not Found</h1><p>The requested resource could not be found.</p></body>
// </html>
// ZZEOF;
// exit(0);
?>
あなたの質問は何ですか?あなたは何を達成しようとしていますか?現在エラーが表示されていますか?もしそうなら、エラーは何ですか? –
私はどんなエラーも受けません。しかし、私のコードが動作していない、画像が読み込まれていません。私は自分の配列の部分を想定しています。 – Saterial
エラーをチェックしていません。 $ _GET ['目的']; 'は何もしません、' intval($ i); 'は何もしません、' $ _GET ['私は]; '何もしません。割り当てを行い、エラー報告を有効にしたり、ログをチェックしたりする必要があります。 – chris85