这个程序是信息发布程序.
可是每次发布的时候.都要上传图象,
留空的话就会提示错误,说上传的文件类型出错.
我不知道是那块出了问题!
代码如下!请大家帮我解决下.急急急!
<?php
// --------------------------------------------------------------------------
// File name : upload.php
// Description : 上传图像
// --------------------------------------------------------------------------
if(!defined('IN_SINFO')) {
exit('Access Denied');
}
$attachment = $_FILES['attachment']['tmp_name'];
$attachment_type = $_FILES['attachment']['type'];
$attachment_size = $_FILES['attachment']['size'];
$attachment_name = $_FILES['attachment']['name'];
if ($attachment_size > 200000) {//图像大小控制200k
showmsg('上传的图像超过200K');
}
if (trim($attachment) != 'none' && trim($attachment) != '' && trim($attachment_name) != '') {
$extension = strtolower(fileext($attachment_name));
$attach_dir = SINFO_ROOT . './attachment';
$filename = md5($timestamp . $attachment_name);//文件名
$imagename = $filename.'.'.$extension;
$target = $attach_dir.'/'.$imagename;
if (function_exists('move_uploaded_file') && @move_uploaded_file($attachment, $target)) {
@chmod ($target, 0666);
$attachment = $target;
} elseif (@copy($attachment, $target)) {
@chmod ($target, 0666);
$attachment = $target;
} elseif (@is_readable($attachment)) {
if ($fp = @fopen($attachment,'rb')) {
@flock($fp,2);
$filedata = @fread($fp,@filesize($attachment));
@fclose($fp);
}
if ($fp = @fopen($target, 'wb')) {
@flock($fp, 2);
@fwrite($fp, $filedata);
@fclose($fp);
@chmod ($target, 0666);
$attachment = $target;
} else {
showmsg('上传图像发生意外错误!');
}
}
}
if (!in_array($extension, array( ' ', 'jpg', 'jpeg', 'gif', 'png')) && !getimagesize($target)) {
@unlink($target);
showmsg('上传的文件不是一个有效的图像文件');
}
//生成微缩图
$tofile = $attach_dir.'/small/'.$filename.'.jpg';
makethumb($target,$tofile);
//获取文件扩展名
function fileext($filename) {
return trim(substr(strrchr($filename, '.'), 1));
}
//生成缩略图
function makethumb($srcfile,$tofile,$width=65,$height=65) {
$data = getimagesize($srcfile);
switch ($data[2]) {
case 1:
$im = imagecreatefromgif($srcfile);
break;
case 2:
$im = imagecreatefromjpeg($srcfile);
break;
case 3:
$im = imagecreatefrompng($srcfile);
break;
}
$srcw = $data[0];
$srch = $data[1];
$width = ($width > $srcw) ? $srcw : $width;
$height = ($height > $srch) ? $srch : $height;
if ($srcw * $width > $srch * $height) {
$height = round($srch * $width / $srcw);
} else {
$width = round($srcw * $height / $srch);
}
if (function_exists("imagecreatetruecolor")) {
$new = imagecreatetruecolor($width, $height);
imagecopyresampled($new, $im, 0, 0, 0, 0, $width, $height, $srcw, $srch);
} else {
$new = imagecreate($width, $height);
imagecopyresized($new, $img, 0, 0, 0, 0, $width, $height, $srcw, $srch);
}
imagejpeg($new,$tofile);
imagedestroy($new);
imagedestroy($im);
}
?>