Authored by Rock Zhang

添加意见反馈图片上传路由

... ... @@ -106,6 +106,136 @@ class Images
return $domain;
}
/**
* 图片上传
* @param string $name 文件表单name, 即用于$_FILES[$name]
*/
public static function saveImage($name)
{
if (empty($_FILES[$name]))
{
return array();
}
$files = $_FILES[$name];
$images = array();
if (is_array($files['tmp_name']))
{
foreach ($files['tmp_name'] as $k => $tmp_name)
{
if(!empty($tmp_name))
{
$images[$files['name'][$k]] = $tmp_name;
}
}
}
else
{
$images[$files['name']] = $files['tmp_name'];
}
if($_SERVER['HTTP_HOST'] != 'test.service.api.yohobuy.com') //代理转接
{
return self::agentCurlImage($images);
}
else
{
return self::uploadStreamImage($images);
}
}
/**
* 上传图片[图片上传域名限制于http://test.service.api.yohobuy.com]
*
* @param string | array(filename => absolute file path) $file
* url:http://upload.static.yohobuy.com?project=sns&fileData=xxx
* @return mixed
*/
public static function uploadStreamImage($file)
{
$end ="\r\n";
$twoHyphens ="--";
$boundary = "*****";
$stream = '';
$files = is_array($file) ? $file : array($file);
foreach($files as $name => $filename)
{
if(!file_exists($filename))
{
continue;
}
$name = is_numeric($name) ? name.'.jpg' : $name;
$stream .= $twoHyphens.$boundary.$end;
$stream .="Content-Disposition: form-data; "."name=\"fileData\";filename=\"".$name ."\"".$end; // form file element name :fileData
$stream .= $end;
$stream .= file_get_contents($filename);
$stream .= $end;
}
if(empty($stream))
{
return false;
}
$stream .= $twoHyphens.$boundary.$end;
$stream .="Content-Disposition: form-data; "."name=\"project\"".$end;
$stream .= $end;
$stream .= "sns";//project sns
$stream .= $end;
$stream .= $twoHyphens .$boundary .$twoHyphens .$end;
$opts = array(
'http' => array(
'method' => 'POST',
'header' => 'content-type:multipart/form-data;boundary='.$boundary,
'content' => $stream
)
);
$context = stream_context_create($opts);
$result = json_decode(file_get_contents('http://upload.static.yohobuy.com', false, $context), true);
if(!empty($result['data']['imagesList']))
{
return count($file) == 1 || !is_array($file) ? current($result['data']['imagesList']) : $result['data']['imagesList'] ;
}
else
{
return false;
}
}
/**
* 代理上传图片
*
* @param array|string $files
* @return array
*/
private static function agentCurlImage($file)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
curl_setopt($ch, CURLOPT_URL, 'http://test.service.api.yohobuy.com/sns/ajax/uploadimg');
curl_setopt($ch, CURLOPT_POST, true);
$params = array();
$files = is_array($file) ? $file : array($file);
foreach($files as $key => $name)
{
$key = is_numeric($key) ? $key.'.jpg' : $key;
$filename = dirname($name).'/'.$key;
rename($name, $filename);
if (@class_exists('\CURLFile'))
{
$params["images[$key]"] = new \CURLFile(realpath($filename));
}
else
{
$params["images[$key]"] = '@' . realpath($filename);
}
}
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$response = json_decode(curl_exec($ch), true);
return $response['data'];
}
/**
* 获取模板的图片地址
* @param $fileName
... ...
... ... @@ -387,6 +387,18 @@ class HomeController extends AbstractAction
}
/**
* 异步上传图片
*/
public function suggestimgUploadAction() {
if ($this->isAjax()) {
$filename = $this->get('filename', '');
$result = \Plugin\Images::saveImage($filename);
$this->echoJson($result);
}
}
/**
* 异步保存意见反馈数据
*/
public function savesuggestAction() {
... ...