您的位置 首页 php

ThinkPHP base64上传图片

最近我们项目需要一台图片服务器存储用户上传的图片,我们使用base64处理和存储图片,下面简单介绍一下如何使用 PHP 上传图片到 服务器,并获取相应图片的返回信息 可以参考一下

一:图片上传判断

 <?php  
/**
* base64图片上传
* @param $base64_img
* @return array
*/
private static function img_upload($base64_img){
$base64_img = trim($base64_img);
$up_dir = '../../../upload/images/interface/' ;

if ( ! file_exists($up_dir)){
mkdir ($up_dir, 0777 );
}

if (preg_match( '/^(data:s*image/(w+);base64,)/' , $base64_img, $result)){
$type = $result[ 2 ];
if (in_array($type, array ( 'pjpeg' , 'jpeg' , 'jpg' , 'gif' , 'bmp' , 'png' ))){
$new_file = $up_dir.date( 'YmdHis_' ).method::getRand ch ar(). '.' .$type;
if (file_put_contents($new_file, base64_decode(str_replace($result[ 1 ], '' , $base64_img)))){
$img_path = str_replace( '../../..' , '' , $new_file);
return array ( 'code' => 1 , ' msg ' => "图片上传成功" , 'url' => $img_path);
}
return array ( 'code' => 2 , 'msg' => "图片上传失败" );
}
//文件类型错误
return array ( 'code' => 4 , 'msg' => "文件类型错误" );
}
//文件错误
return array ( 'code' => 3 , 'msg' => "文件错误" );
}
 二:图片上传到服务器 

// 关键在这里!

$post_data = file _get_contents($realpath); // raw_post方式

// 如果是是一个数组,则content_type自动为multipart/form-data

// $post_data = array(

// ‘file’ => “@$realpath”

// );

$headers = array();

$headers[] = ‘Content-Type:’.$ext; // 还有这里!

curl_setopt($ch, CURLOPT_URL, $zimg_upload_url);

curl_setopt($ch, CURLOPT_HEADER, false );

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

//启用时会发送一个常规的POST请求,类型为:application/x-www-form-urlencoded,就像表单提交的一样。

curl_setopt($ch, CURLOPT_POST, true);

curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);

curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

$info = curl_exec($ch);

curl_close($ch);

$ json = json_decode($info, true);

$signature = $json[‘info’][‘md5’];

$imgurl = $zimg_domain . $signature;

return array(‘imgurl’=>$imgurl,’imgcode’=>$signature);

$post_data = file_get_contents($realpath); // raw_post方式

// 如果是是一个数组,则content_type自动为multipart/form-data

// $post_data = array(

// ‘file’ => “@$realpath”

// );

$headers = array();

$headers[] = ‘Content-Type:’.$ext; // 还有这里!

curl_setopt($ch, CURLOPT_URL, $zimg_upload_url);

curl_setopt($ch, CURLOPT_HEADER, false);

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

//启用时会发送一个常规的POST请求,类型为:application/x-www-form-urlencoded,就像表单提交的一样。

curl_setopt($ch, CURLOPT_POST, true);

curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);

curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

$info = curl_exec($ch);

curl_close($ch);

$json = json_decode($info, true);

$signature = $json[‘info’][‘md5’];

$imgurl = $zimg_domain . $signature;

return array(‘imgurl’=>$imgurl,’imgcode’=>$signature);

三:判断

<?php

$upFile = $_FILES [ 'file' ];

/**

* 创建文件夹函数,用于创建保存文件的文件夹

* @param str $dirPath 文件夹名称

* @return str $dirPath 文件夹名称

*/

function creaDir( $dirPath ){

$curPath = dirname( __FILE__ );

$path = $curPath . '\\' . $dirPath ;

if ( is_dir ( $path ) || mkdir ( $path ,0777,true)) {

return $dirPath ;

}

}

//判断文件是否为空或者出错

if ( $upFile [ 'error' ]==0 && ! empty ( $upFile )) {

$dirpath = creaDir( 'upload' );

$filename = $_FILES [ 'file' ][ 'name' ];

$queryPath = './' . $dirpath . '/' . $filename ;

//move_uploaded_file将浏览器缓存file转移到服务器文件夹

if (move_uploaded_file( $_FILES [ 'file' ][ 'tmp_name' ], $queryPath )){

echo $queryPath ;

}

}

?>

文章来源:智云一二三科技

文章标题:ThinkPHP base64上传图片

文章地址:https://www.zhihuclub.com/150527.shtml

关于作者: 智云科技

热门文章

网站地图