Example #1
0
/**
 * 遍历获取目录下的指定类型的文件
 * @param $path
 * @param array $files
 * @return array
 */
function getfiles($path, &$files = array())
{
    if (!is_dir($path)) {
        return null;
    }
    $handle = opendir($path);
    while (false !== ($file = readdir($handle))) {
        if ($file != '.' && $file != '..') {
            $path2 = $path . '/' . $file;
            if (is_dir($path2)) {
                getfiles($path2, $files);
            } else {
                if (preg_match("/\\.(gif|jpeg|jpg|png|bmp)\$/i", $file)) {
                    $path2 = str_replace(getServerRootPath(), "", $path2);
                    $files[] = $path2;
                }
            }
        }
    }
    return $files;
}
Example #2
0
/**
 * 远程抓取
 * @param $uri
 * @param $config
 */
function getRemoteImage($uri, $config)
{
    //忽略抓取时间限制
    set_time_limit(0);
    //ue_separate_ue  ue用于传递数据分割符号
    $imgUrls = explode("ue_separate_ue", $uri);
    $tmpNames = array();
    foreach ($imgUrls as $imgUrl) {
        //http开头验证
        if (strpos($imgUrl, "http") !== 0) {
            array_push($tmpNames, "error");
            continue;
        }
        //获取请求头
        $heads = get_headers($imgUrl);
        //死链检测
        if (!(stristr($heads[0], "200") && stristr($heads[0], "OK"))) {
            array_push($tmpNames, "error");
            continue;
        }
        //格式验证(扩展名验证和Content-Type验证)
        $fileType = strtolower(strrchr($imgUrl, '.'));
        if (!in_array($fileType, $config['allowFiles']) || stristr($heads['Content-Type'], "image")) {
            array_push($tmpNames, "error");
            continue;
        }
        //打开输出缓冲区并获取远程图片
        ob_start();
        $context = stream_context_create(array('http' => array('follow_location' => false)));
        //请确保php.ini中的fopen wrappers已经激活
        readfile($imgUrl, false, $context);
        $img = ob_get_contents();
        ob_end_clean();
        //大小验证
        $uriSize = strlen($img);
        //得到图片大小
        $allowSize = 1024 * $config['maxSize'];
        if ($uriSize > $allowSize) {
            array_push($tmpNames, "error");
            continue;
        }
        //创建保存位置
        $savePath = $config['savePath'];
        if (!file_exists($savePath)) {
            mkdir("{$savePath}", 0777);
        }
        //写入文件
        $tmpName = $savePath . rand(1, 10000) . time() . strrchr($imgUrl, '.');
        try {
            $fp2 = @fopen(getServerRootPath() . $tmpName, "a");
            fwrite($fp2, $img);
            fclose($fp2);
            array_push($tmpNames, $tmpName);
        } catch (Exception $e) {
            array_push($tmpNames, "error");
        }
    }
    /**
     * 返回数据格式
     * {
     *   'url'   : '新地址一ue_separate_ue新地址二ue_separate_ue新地址三',
     *   'srcUrl': '原始地址一ue_separate_ue原始地址二ue_separate_ue原始地址三',
     *   'tip'   : '状态提示'
     * }
     */
    echo "{'url':'" . implode("ue_separate_ue", $tmpNames) . "','tip':'远程图片抓取成功!','srcUrl':'" . $uri . "'}";
}
Example #3
0
 /**
  * 按照日期自动创建存储文件夹
  * @return string
  */
 private function getFolder()
 {
     $pathStr = getServerRootPath() . $this->getRelativePath();
     if (!file_exists($pathStr)) {
         if (!mkdir($pathStr, 0777, true)) {
             return false;
         }
     }
     return $pathStr;
 }