forked from fex-team/ueditor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetRemoteImage.php
More file actions
executable file
·100 lines (96 loc) · 3.73 KB
/
Copy pathgetRemoteImage.php
File metadata and controls
executable file
·100 lines (96 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
/**
* Created by JetBrains PhpStorm.
* User: taoqili
* Date: 11-12-28
* Time: 上午9:54
* To change this template use File | Settings | File Templates.
*/
header("Content-Type: text/html; charset=utf-8");
error_reporting(E_ERROR|E_WARNING);
//远程抓取图片配置
$config = array(
"savePath" => "upload/" , //保存路径
"allowFiles" => array( ".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp" ) , //文件允许格式
"maxSize" => 3000 //文件大小限制,单位KB
);
$uri = htmlspecialchars( $_POST[ 'upfile' ] );
$uri = str_replace( "&" , "&" , $uri );
getRemoteImage( $uri,$config );
/**
* 远程抓取
* @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 // don't follow redirects
)
)
);
//请确保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( $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 . "'}";
}