我想弄清楚如何在PHP中將圖像類型設置爲變量。我需要從數據庫中提取圖像,找到它的圖像類型並顯示它。目前它只顯示JPG格式。獲取圖像類型,PHP
,用來上傳的代碼是:
if(isset($_FILES['image']) && $_FILES['image']['tmp_name'] != '') {
$tempext = explode('.', $_FILES['image']['name']);
$tempext = strtolower($tempext[count($tempext) - 1]);
switch($tempext) {
case 'jpg':
case 'jpeg':
$ext = '.jpg';
break;
case 'gif':
$ext = '.gif';
break;
case 'png':
$ext = '.png';
break;
default:
$ext = false;
}
if($ext != false && move_uploaded_file($_FILES['image']['tmp_name'], $_SERVER['DOCUMENT_ROOT'] . '/img/profiles/' . $id . $ext)) {
$imagesuccess = chmod($_SERVER['DOCUMENT_ROOT'] . '/img/profiles/' . $id . $ext, 0644) ? 'yes' : 'no';
} else {
$imagesuccess = 'no';
}
}
編輯:
我做了一點點的進步,主要是清理一些愚蠢的錯誤,但我仍然沒有得到任何圖像出現。
下面是更新開關情況:
switch ($ext) {
case 'jpg':
case 'jpeg':
$ext = '.jpg';
break;
case 'gif':
$ext = '.gif';
break;
case 'png':
$ext = '.png';
}
和圖像位置:
if(!file_exists($_SERVER['DOCUMENT_ROOT'] . $imgurl . 'profiles/' . $r['id'] . $ext)) {
echo '<img src="' . $imgurl . 'profiles/unavailable.gif" id="profile-big-img" />' . PHP_EOL;
} else {
echo '<p><img class="profileimg" src="' . $imgurl . 'profiles/' . $r['id'] . $ext . 'id="profile-big-img" alt="' . $r['forename'] . ' ' . $r['surname'] . (($r['course'] == 'graphics') ? ' Graphic Design' : ' Illustration') . '" /></p>' . PHP_EOL;
}
你有機會看看最終的HTML嗎? – 2012-03-21 15:32:14
$ tempext來自show time? – 2012-03-21 15:32:44
我建議立即關閉這類問題,以避免產生HTML。任何理智的社區都會接受它。當然不是這個。 – 2012-03-21 15:34:08