我的指令已被註釋掉,我試圖按照概述進行編碼。學習PHP數組和開關
我覺得我把變量傳遞給intval錯誤,錯誤地實現了switch方法,並且檢查是否不是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
您不檢查錯誤。 ''switch(purpose){'是一個錯誤,'case a:'是一個錯誤..'$ _GET ['purpose'];'什麼也不做,'intval($ i);'什麼都不做,'$ _GET [我'];'什麼都不做。您需要分配,並啓用錯誤報告和/或檢查您的日誌。 – chris85