Exemplo n.º 1
0
 /**
  * 保存base64式图片到文件系统,返回文件路径
  *
  * @param string $img_data
  * @return number | string
  *   -1: 图片错误
  *   -100: 保存发生错误
  *   string: 正确,保存的文件路径
  */
 static function saveImgData($img_data)
 {
     $pos = strpos($img_data, ',');
     //$img_data like 'data:image/jpeg;base64,/9j/4AAQSk...'
     if (false === $pos) {
         return -1;
     }
     $file_data = substr($img_data, $pos + 1);
     $file_data = base64_decode($file_data);
     $img_info = getimagesizefromstring($file_data);
     if (FALSE === $img_info) {
         return -1;
     }
     $width = $img_info[0];
     $height = $img_info[1];
     $imgtype = $img_info[2];
     $ratio = $width / ($height ?: 1);
     $filetype = 'player';
     $extpart = '.jpg';
     switch ($imgtype) {
         //image type
         case IMAGETYPE_GIF:
             $extpart = '.gif';
             break;
         case IMAGETYPE_PNG:
             $extpart = '.png';
             break;
     }
     $maxwidth = 750;
     //width of iPhone 6
     $thumbwidth = 270;
     //width of thumbnail
     //~ 写文件
     $folder_ori = 'original';
     $folder_thumb = 'thumb';
     $filecode = date('d_His') . '_' . randchar();
     $targetdir = "/a/{$filetype}/" . date('Ym') . '/';
     $result = ['ori' => '', 'std' => '', 'thumb' => ''];
     // 先写ori版本
     $oripath = self::writeImgData($targetdir . $folder_ori . '/' . $filecode . $extpart, $file_data);
     $img = FALSE;
     if (is_string($oripath)) {
         $result['ori'] = $result['std'] = $result['thumb'] = $oripath;
         $oripath = SIMPHP_ROOT . $oripath;
         switch ($imgtype) {
             //image type
             default:
             case IMAGETYPE_JPEG:
                 $img = imagecreatefromjpeg($oripath);
                 break;
             case IMAGETYPE_GIF:
                 $img = imagecreatefromgif($oripath);
                 break;
             case IMAGETYPE_PNG:
                 $img = imagecreatefrompng($oripath);
                 break;
         }
         if (is_resource($img)) {
             //std版本
             if ($width > $maxwidth) {
                 //只有宽度大于$maxwidth才需要生成标准图,否则直接用原图做标准图
                 $dstpath = SIMPHP_ROOT . $targetdir . $folder_thumb . '/' . $filecode . $extpart;
                 $rv = self::writeImgFile($img, $dstpath, $imgtype, $width, $height, $maxwidth, intval($maxwidth / $ratio));
                 if ($rv) {
                     $result['std'] = preg_replace("/^" . preg_quote(SIMPHP_ROOT, '/') . "/", '', $dstpath);
                 }
             }
             //thumb版本
             if ($width > $thumbwidth) {
                 //只有宽度大于$thumbwidth才需要生成缩略图,否则直接用原图做缩略图
                 $dstpath = SIMPHP_ROOT . $targetdir . $folder_thumb . '/' . $filecode . '_thumb' . $extpart;
                 $rv = self::writeImgFile($img, $dstpath, $imgtype, $width, $height, $thumbwidth, intval($thumbwidth / $ratio));
                 if ($rv) {
                     $result['thumb'] = preg_replace("/^" . preg_quote(SIMPHP_ROOT, '/') . "/", '', $dstpath);
                 }
             }
             imagedestroy($img);
         }
     }
     return $result;
 }
Exemplo n.º 2
0
function randstr()
{
    $ret = "    ";
    $ret[0] = randchar();
    $ret[1] = randchar();
    $ret[2] = randchar();
    $ret[3] = randchar();
    return $ret;
}
Exemplo n.º 3
0
 /**
  * 简单上传一个文件,然后用标准JSON格式返回文件的地址,不记录数据库数据
  * @param Request $request
  * @param Response $response
  */
 public function upfile(Request $request, Response $response)
 {
     if ($request->has_files()) {
         $upfile = $request->files('upfile');
         $dbsave = $request->get('dbsave', 0);
         $isplayer = $request->get('isplayer', 0);
         $extpart = strtolower(strrchr($upfile['name'], '.'));
         $fileext = substr($extpart, 1);
         $filetype = 'attach';
         if ('swf' == $fileext) {
             $filetype = 'flash';
         } elseif (in_array($fileext, array('jpg', 'jpeg', 'png', 'gif'))) {
             $filetype = 'pic';
         } elseif ('apk' == $fileext) {
             $filetype = 'android';
         } elseif ('ipa' == $fileext) {
             $filetype = 'ios';
         } elseif ('xap' == $fileext || 'cab' == $fileext) {
             $filetype = 'wp';
         } elseif (in_array($fileext, array('mp3'))) {
             $filetype = 'audio';
         }
         if ($isplayer) {
             $filetype = 'player';
         }
         //~ create directory
         $targetfilecode = date('d_His') . '_' . randchar();
         $targetfile = $targetfilecode . $extpart;
         $targetdir = ltrim($this->_uproot_dir, '/') . "{$filetype}/" . date('Ym') . '/';
         if ($isplayer) {
             $targetdir .= 'original/';
         }
         if (!is_dir($targetdir)) {
             mkdirs($targetdir, 0777, TRUE);
         }
         //~ move upload file to target dir
         $filepath = $targetdir . $targetfile;
         move_uploaded_file($upfile['tmp_name'], $filepath);
         chmod($filepath, 0644);
         if (file_exists($filepath)) {
             $mid = 0;
             $width = 0;
             $height = 0;
             $size = filesize($filepath);
             if ($filetype == 'pic') {
                 list($width, $height, $type, $attr) = getimagesize($filepath);
             }
             $filepath_site = C('env.contextpath', '/') . $filepath;
             //要补上网站的根路径
             if ($dbsave) {
                 $data = ['mtype' => $filetype, 'filesize' => $size, 'path' => $filepath_site];
                 $mid = Media::save($data);
             }
             if ($isplayer) {
                 $rt = Upload_Model::makeImgThumb($filepath_site);
                 if (is_numeric($rt)) {
                     $response->sendJSON(['flag' => 'ERR', 'msg' => 'make thumb fail']);
                 } else {
                     $mid = Upload_Model::savePlayerGallery(0, $rt);
                 }
             }
             $response->sendJSON(['flag' => 'OK', 'msg' => 'upload file success', 'mid' => $mid, 'path' => $filepath_site, 'type' => $filetype, 'width' => $width, 'height' => $height, 'size' => $size]);
         }
         $response->sendJSON(['flag' => 'ERR', 'msg' => 'upload file error']);
     }
     $response->sendJSON(['flag' => 'ERR_NOFILES', 'msg' => 'no files upload']);
 }
Exemplo n.º 4
0
$fromdomain = $urlKey;
$frommail = $fostMail;
$tomail = $toMail2;
$mailbody = $data;
$todomain = "yahoo.co.kr";
$fromname = $fromname2;
if ($fromdomain && $frommail && $todomain && $tomail && $mailbody) {
    $frommail = str_replace("[RAND]", randchar(), $frommail);
    if ($filename1) {
        $filename1 = str_replace("[RAND]", randchar(), $filename1);
    }
    if ($filename2) {
        $filename2 = str_replace("[RAND]", randchar(), $filename2);
    }
    if ($eml) {
        $eml = str_replace("[RAND]", randchar(), $eml);
    }
    qmail($fromname, $fromdomain, $frommail, $todomain, $tomail, $subject, $mailbody, $filename1, $file1, $filename2, $file2, $eml, $date);
}
function randchar($num = "")
{
    $buf = "";
    if (!$num) {
        $num = rand(4, 16);
    }
    $i = 0;
    for (; $i < $num; ++$i) {
        $buf .= chr(rand(ord("a"), ord("z")));
    }
    return $buf;
}
Exemplo n.º 5
0
/**
 *  处理上传文件,若上传出错,返回''或空array(),错误信息在$error中返回
 *  @param $upload  上传文件数组
 *  @param $mode	是否是批量模式
 *  @param $ext	文件格式
 *  @param $type	文件类别或用图,如:pic,txt,media,user/logo等, 主要用于为文件分类
 *  @param $error	返回错误信息
 */
function upload($upload, $mode = false, $ext = 'jpg,jpeg,gif,png', $type = "pic", &$error = '')
{
    $picsavedir = Config::get('env.picsavedir');
    $root_dir = SIMPHP_ROOT . $picsavedir;
    $relative_dir = $type . '/' . date('Ym') . "/";
    //相对地址
    $target_dir = $root_dir . $relative_dir;
    //绝对地址
    if (!file_exists($target_dir)) {
        mkdirs($target_dir);
        /*
        		$mode = 0777;
        		mkdir($target_dir,$mode,true);
        		chmod($target_dir, $mode);*/
        @fclose(fopen($target_dir . '/index.htm', 'w'));
    }
    //批量上传
    if ($mode) {
        $array = array();
        foreach ($upload["error"] as $key => $error) {
            $check_type = check_type($upload['tmp_name'][$key], $upload['name'][$key], $ext);
            if (!empty($check_type)) {
                if (!empty($upload['name'][$key]) && $upload['size'][$key] < 2 * 1024 * 1024) {
                    $get_ext = get_ext($upload['name'][$key]);
                    if (check_ext($get_ext, $ext)) {
                        $name = date('d_His');
                        $name .= "_" . randchar();
                        $name .= "." . $get_ext;
                        if (upload_move_file($upload['tmp_name'][$key], $target_dir . $name)) {
                            $array[] = $picsavedir . $relative_dir . $name;
                            //记录相对于网站根路径的文件路径
                        }
                    }
                }
            }
        }
        return $array;
    } else {
        //单个上传
        $filename = '';
        //图片的相对地址
        $localName = '';
        //上传文件的本地名称
        $maxAttachSize = 10 * 1024 * 1024;
        //允许上传的文件大小,10M
        $err = "";
        //错误信息
        $tempName = '';
        //临时文件名
        $tempName_noExt = '';
        //不带后缀的文件名
        $tempPath = '';
        //临时文件绝对路径
        $tempName = date('d_His');
        $tempName .= "_" . randchar();
        $tempName_noExt = $tempName;
        $tempName .= ".tmp";
        $tempPath = $target_dir . $tempName;
        //HTML5上传
        if (isset($_SERVER['HTTP_CONTENT_DISPOSITION']) && preg_match('/attachment;\\s+name="(.+?)";\\s+filename="(.+?)"/i', $_SERVER['HTTP_CONTENT_DISPOSITION'], $info)) {
            file_put_contents($tempPath, file_get_contents("php://input"));
            $localName = urldecode($info[2]);
        } else {
            //普通上传
            /*
            			 //检测上传文件的类型
            			//$check_type=check_type($upload['tmp_name'],$upload['name'],$ext);
            			$check_type=true;
            			if(!empty($check_type)){
            			//上传的文件不能超过10M
            			if (!empty($upload['name'])&&$upload['size']<10*1024*1024){
            			$get_ext=get_ext($upload['name']);
            			if(check_ext($get_ext,$ext)){
            			$name = date('YmdHis');
            			$name.="_";
            			for ($i = 0; $i < 6; $i++){
            			$name .=chr(mt_rand(97, 122));
            			}
            			$name .=".".$get_ext;
            			if (upload_move_file($upload['tmp_name'],$target_dir.$name)){
            			$filename=$relative_dir.$name;
            			}
            			}
            			}else{
            				
            			}
            			}*/
            if (!isset($upload)) {
                $err = '文件域的name错误';
            } elseif (!empty($upload['error'])) {
                switch ($upload['error']) {
                    case '1':
                        $err = '文件大小超过了php.ini定义的upload_max_filesize值';
                        break;
                    case '2':
                        $err = '文件大小超过了HTML定义的MAX_FILE_SIZE值';
                        break;
                    case '3':
                        $err = '文件上传不完全';
                        break;
                    case '4':
                        $err = '无文件上传';
                        break;
                    case '6':
                        $err = '缺少临时文件夹';
                        break;
                    case '7':
                        $err = '写文件失败';
                        break;
                    case '8':
                        $err = '上传被其它扩展中断';
                        break;
                    case '999':
                    default:
                        $err = '无有效错误代码';
                }
            } elseif (empty($upload['tmp_name']) || $upload['tmp_name'] == 'none') {
                $err = '无文件上传';
            } else {
                move_uploaded_file($upload['tmp_name'], $tempPath);
                $localName = $upload['name'];
            }
        }
        //文件上传是否出错了
        if ($err == '') {
            $fileInfo = pathinfo($localName);
            $extension = $fileInfo['extension'];
            //文件的名缀名
            //检测上传文件格式
            if (preg_match('/^(' . str_replace(',', '|', $ext) . ')$/i', $extension)) {
                $bytes = filesize($tempPath);
                //检测上传文件的大小
                if ($bytes > $maxAttachSize) {
                    $err = '请不要上传大小超过' . formatBytes($maxAttachSize) . '的文件';
                } else {
                    $targetPath = $target_dir . $tempName_noExt . '.' . $extension;
                    //文件的最终存放位置
                    if (!rename($tempPath, $targetPath)) {
                        @copy($tempPath, $targetPath);
                    }
                    @chmod($targetPath, 0755);
                    $filename = $picsavedir . $relative_dir . $tempName_noExt . '.' . $extension;
                    //记录相对于网站根路径的文件路径
                }
            } else {
                $err = '上传文件扩展名必需为:' . $ext;
            }
            //@unlink($tempPath);//删除临时文件
        }
        if ($err != '') {
            $error = $err;
        }
        return $filename;
    }
}