Last active
December 18, 2015 14:09
-
-
Save guweigang/5795258 to your computer and use it in GitHub Desktop.
upload file using fsocketopen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* php fsockopen上传,如果使用https,必须先开启ssl才能使用 | |
* @param str $url 要上传的url | |
* @param array $files 上传的文件参数 数组格式 | |
* array( | |
* 0=> array( | |
* "name" => "图片0.jpg", //显示的文件名 | |
* "type" => "image/jpeg", //文件mime | |
* "savepath" => "D:/www/uploadfile/201305", //文件所在的目录 | |
* "savename" => "images0.jpg" // 真实的文件名 | |
* ) | |
* ) | |
* @param array $post 其它的表单 由key =>val 对组成 | |
* @return url响应的内容 | |
*/ | |
function php_upload($url, $files, $post = array()) { | |
$str = ""; | |
$data = ""; | |
//解析url | |
$parse_url = parse_url($url); | |
if (!isset($parse_url["scheme"])) { | |
$parse_url["scheme"] = "http"; | |
} | |
if (!isset($parse_url["host"])) { | |
$parse_url["host"] = "localhost"; | |
} | |
if (!isset($parse_url["port"])) { | |
$parse_url["port"] = $parse_url["scheme"] == "https" ? "443" : "80"; | |
} | |
if (!isset($parse_url["query"])) { | |
$parse_url["query"] = ""; | |
} else { | |
$parse_url["query"] = "?" . $parse_url["query"]; | |
} | |
$parse_url["protocol"] = $parse_url["scheme"] == "https" ? "ssl://" : "tcp://"; | |
$boundary = "---------------------" . substr(md5(rand(0, 32000)), 0, 10); | |
//拼接header头 | |
$header = "POST " . $parse_url["path"] . $parse_url["query"] . " HTTP/1.0\r\n"; | |
$header .= "Host: {$parse_url["host"]}\r\n"; | |
$header .= "Content-type: multipart/form-data, boundary=$boundary\r\n"; | |
//拼接Post | |
foreach ($post AS $index => $value) { | |
$data .="--$boundary\r\n"; | |
$data .= "Content-Disposition: form-data; name=\"" . $index . "\"\r\n"; | |
$data .= "\r\n" . $value . "\r\n"; | |
$data .="--$boundary\r\n"; | |
} | |
//拼接要上传的文件 | |
foreach ($files as $k => $file) { | |
$content_file = file_get_contents($file['savepath'] . $file['savename']); | |
$data .= "--$boundary\r\n"; | |
$data .="Content-Disposition: form-data; name=\"file_{$k}\"; filename=\"{$file['name']}\"\r\n"; | |
$data .= "Content-Type: {$file['type']}\r\n\r\n"; | |
$data .= "" . $content_file . "\r\n"; | |
$data .="--$boundary--\r\n"; | |
} | |
$header .= "Content-length: " . strlen($data) . "\r\n\r\n"; | |
//连接到目标主机,并获取返回结果 | |
$fp = fsockopen($parse_url["protocol"].$parse_url["host"], $parse_url["port"]); | |
stream_set_blocking($fp, true); | |
stream_set_timeout($fp, 15); | |
$status = stream_get_meta_data($fp); | |
@fputs($fp, $header . $data); | |
if (!$status['timed_out']) { | |
while (!feof($fp)) { | |
if (($header = @fgets($fp)) && ($header == "\r\n" || $header == "\n")) { | |
break; | |
} | |
} | |
while (!feof($fp)) { | |
$str.=fgets($fp, 128); | |
} | |
} | |
fclose($fp); | |
return $str; | |
} | |
//使用示例 | |
$file=array( | |
0=> array( | |
"name" => "图片0.jpg", //显示的文件名 | |
"type" => "image/jpeg", //文件mime | |
"savepath" => "D:/www/uploadfile/201305", //文件所在的目录 | |
"savename" => "images0.jpg" // 真实的文件名 | |
) | |
1=> array( | |
"name" => "图片1.jpg", //显示的文件名 | |
"type" => "image/jpeg", //文件mime | |
"savepath" => "D:/www/uploadfile/201305", //文件所在的目录 | |
"savename" => "images1.jpg" // 真实的文件名 | |
) | |
) | |
echo php_upload("http://locahost/upload.php", $file, array("svae_path" => "uploadfile")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment