【php开源代码栏目提醒】:文章导读:在新的一年中,各位网友都进入紧张的学习或是工作阶段。网学会员整理了php开源代码-uploadfiles.php的相关内容供大家参考,祝大家在新的一年里工作和学习顺利!
<?php
if(!defined('ROOT') || !isset($php_self) || !preg_match("/[\/\\\\]admincp\.php$/", $php_self)) {
exit('Access Denied');
}
$attachments = $attachs = $attach_data = array();
if(isset($_FILES['attach']) && is_array($_FILES['attach'])) {
foreach($_FILES['attach'] as $key => $var) {
foreach($var as $id => $val) {
$attachments[$id][$key] = $val;
}
}
}
if($attachments) {
$gd_version = gd_version();
foreach($attachments as $key => $attach) {
if(!disuploadedfile($attach['tmp_name']) || !($attach['tmp_name'] != 'none' && $attach['tmp_name'] && $attach['name'])) {
continue;
}
$attach['name'] = strtolower($attach['name']);
$attach['ext'] = getextension($attach['name']);
$fnamehash = md5(uniqid(microtime()));
// 鍒ゆ柇涓婁紶鐩綍鐨勬柟寮?
switch($base['attachments_save_dir']) {
case 0: $attachsubdir = '/'; break; //鍏ㄩ儴鏀句竴璧?
case 1: $attachsubdir = '/cate_'.$cid.'/'; break; //鎸夊垎绫绘斁
case 2: $attachsubdir = '/date_'.formatdate('Ym').'/'; break; //鎸夋湀鏀?
case 3: $attachsubdir = '/ext_'.$attach['ext'].'/'; break; //鎸夋枃浠剁被鍨?
}
// 鍙栧緱闄勪欢鐩綍鐨勭粷瀵硅矾寰?
$attach_dir = '../'.$base['attachments_dir'].$attachsubdir;
if(!is_dir($attach_dir)) {
mkdir($attach_dir, 0777);
fclose(fopen($attach_dir.'/index.htm', 'w'));
@chmod($attach_dir, 0777);
}
// 鍒ゆ柇涓婁紶鐨勭被鍨?
// path鍙橀噺涓虹鐞嗙洰褰曠浉瀵硅矾寰?鍚庡彴鎿嶄綔鐢?
// filepath鍙橀噺涓鸿窡鐩綍鐩稿璺緞,鍓嶅彴璇诲彇鐢?
// fnamehash鍙橀噺涓哄綋鍓嶆椂闂寸殑MD5鏁e垪,閲嶅懡鍚嶉檮浠跺悕
if (!in_array($attach['ext'], array('gif', 'jpg', 'jpeg', 'png'))) {
$path = $attach_dir.$fnamehash.'.file';
$filepath = $attachsubdir.$fnamehash.'.file';
} else {
$path = $attach_dir.$fnamehash.'.'.$attach['ext'];
$filepath = $attachsubdir.$fnamehash.'.'.$attach['ext'];
}
$attachment = upfile($attach['tmp_name'], $path);
// 濡傛灉涓绉嶅嚱鏁颁笂浼犲け璐ワ紝杩樺彲浠ョ敤鍏朵粬鍑芥暟涓婁紶
if (!$attachment) {
redirect('涓婁紶闄勪欢鍙戠敓鎰忓閿欒!');
}
$tmp_filesize = @filesize($attachment);
if ($tmp_filesize != $attach['size']) {
@unlink($attachment);
redirect('涓婁紶闄勪欢鍙戠敓鎰忓閿欒!');
}
// 鍒ゆ柇鏄惁涓哄浘鐗囨牸寮?
if (in_array($attach['ext'], array('gif', 'jpg', 'jpeg', 'png'))) {
if ($imginfo=@getimagesize($attachment)) {
if (!$imginfo[2] || !$imginfo['bits']) {
@unlink($attachment);
redirect('涓婁紶鐨勬枃浠朵笉鏄竴涓湁鏁堢殑GIF鎴栬匤PG鏂囦欢!');
} else {
$isimage = '1';
}
}
// 鍒ゆ柇鏄惁浣跨敤缂╃暐鍥?
if ($base['attachments_thumbs'] && $gd_version) {
$size = explode('x', strtolower($base['attachments_thumbs_size']));
if (($imginfo[0] > $size[0] || $imginfo[1] > $size[1]) && $attach['size'] < 2048000) {
$attach_thumb = array(
'filepath' => $attachment,
'filename' => $fnamehash,
'extension' => $attach['ext'],
'attachsubdir' => $attachsubdir,
'thumbswidth' => $size[0],
'thumbsheight' => $size[1],
);
$thumb_data = generate_thumbnail($attach_thumb);
$attach_data['thumbwidth'] = $thumb_data['thumbwidth'];
$attach_data['thumbheight'] = $thumb_data['thumbheight'];
$attach_data['thumbfilepath'] = $attachsubdir.$thumb_data['thumbfilepath'];
}
}
//姘村嵃
$watermark_size = explode('x', strtolower($base['watermark_size']));
if($isimage && $base['watermark'] && $imginfo[0] > $watermark_size[0] && $imginfo[1] > $watermark_size[1] && $attach['size'] < 2048000) {
require_once(ROOT.'include/func_image.php');
create_watermark($path);
$attach['size'] = filesize($path);
}
}
// 鎶婃枃浠朵俊鎭彃鍏ユ暟鎹簱
$DB->query("INSERT INTO {$db_prefix}attachments (filename,filesize,filetype,filepath,dateline,downloads,isimage,thumb_filepath,thumb_width,thumb_height) VALUES ('".addslashes($attach['name'])."', '".$attach['size']."', '".addslashes($attach['type'])."', '".addslashes($filepath)."', '$timestamp', '0', '$isimage', '".$attach_data['thumbfilepath']."', '".$attach_data['thumbwidth']."','".$attach_data['thumbheight']."')");
$aidtmp = $DB->i