【发布时间】:2012-03-22 03:38:38
【问题描述】:
我试图弄清楚如何在 PHP 中将图像类型设置为变量。我需要从数据库中提取图像,找到它的图像类型并显示它。目前它只显示jpg的。
上传代码为:
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?
-
演出时 $tempext 来自哪里?
-
我建议立即关闭此类问题,因为缺少生成的 HTML。任何理智的社区都会接受它。当然不是这个。
-
是的,目前我收到一个警告:“警告:exif_imagetype() [function.exif-imagetype]:/Applications/XAMPP/xamppfiles/htdocs/gdi/ 中的文件名不能为空profile.php on line 174" 我猜是指
$exifType变量中的['temp_name']?我不是 PHP 开发人员,所以调试这个有点任务。 -
如果我取出变量和switch语句,jpg显示正常,gif和png显示为不可用.gif
标签: php