Created
June 3, 2019 15:31
-
-
Save tingv/87c7357b8ae5a73ad31141923873fed5 to your computer and use it in GitHub Desktop.
MWeb 本地图片上传到 Github 服务
This file contains hidden or 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 | |
/** | |
* MWeb 本地图片上传到 Github 服务 | |
* User: TingV | |
* Date: 2019-05-31 | |
* Time: 06:10 | |
*/ | |
$token = $_SERVER['HTTP_TOKEN']; // 个人访问令牌 [ 必须 ] 获取地址: https://github.com/settings/tokens | |
$owner = $_SERVER['HTTP_OWNER']; // Github 用户名 [ 必须 ] | |
$repo = $_SERVER['HTTP_REPO']; // 仓库名 [ 必须 ] | |
$branch = isset($_SERVER['HTTP_BRANCH']) ? $_SERVER['HTTP_BRANCH'] : 'master'; // 分支 [ 默认: master ] | |
$file_name = $_FILES['file']['name']; | |
$tmp_name = $_FILES['file']['tmp_name']; | |
if ( $_FILES['file']['error'] !== UPLOAD_ERR_OK ){ | |
die("图片 " . $file_name . " 上传失败"); | |
} | |
$extension = strtolower(pathinfo($file_name, PATHINFO_EXTENSION)); // 获取文件后缀 | |
if ( !in_array($extension, ['gif', 'jpg', 'jpeg', 'bmp', 'png', 'swf']) ) { | |
die("图片 " . $file_name . " 后缀非法"); | |
} | |
$save_name = date('YmdHis') . '.' . $extension; // 保存的文件名 | |
$upload_path = date('Y') . DIRECTORY_SEPARATOR . date('m') . DIRECTORY_SEPARATOR . date('d'); // 上传目录 | |
$tmp_save_file = __DIR__ . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . $owner . DIRECTORY_SEPARATOR . $repo . DIRECTORY_SEPARATOR . $branch . DIRECTORY_SEPARATOR . $save_name; // 临时保存文件 | |
$tmp_save_path = dirname($tmp_save_file); // 临时保存目录 | |
!is_dir($tmp_save_path) && mkdir($tmp_save_path, 0755, true); // 创建临时保存目录 | |
if( move_uploaded_file($tmp_name, $tmp_save_file) == false ){ | |
die("图片 " . $file_name . " 移动失败"); | |
} | |
if ( !in_array(getImageType($tmp_save_file), [1, 2, 3, 4, 6, 13]) ){ | |
@unlink($tmp_save_file); | |
die("图片 " . $file_name . " 类型非法"); | |
} | |
$content = base64EncodeImage($tmp_save_file); | |
$headers = [ | |
'Authorization: token ' . $token, | |
'Content-Type: application/json', | |
'User-Agent: ' . (isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'MWeb/3.2.3 (Mac OS X ban ben 10.14.5(ban hao 18F132))'), | |
]; | |
$data = json_encode([ | |
'branch'=> $branch, | |
'message'=> isset($_SERVER['HTTP_MESSAGE']) ? $_SERVER['HTTP_MESSAGE'] : 'Upload by MWeb', | |
'content'=> $content, | |
]); | |
$url = "https://api.github.com/repos/{$owner}/{$repo}/contents/{$upload_path}/{$save_name}"; | |
$data = request($url, $headers, $data); | |
@unlink($tmp_save_file); | |
header('Content-Type:application/json; charset=utf-8'); | |
die($data); // 输出 Github 返回值 | |
function request($url, $headers, $data) | |
{ | |
$handle = curl_init(); | |
curl_setopt($handle,CURLOPT_URL, $url); | |
curl_setopt($handle,CURLOPT_HEADER,0); | |
curl_setopt($handle,CURLOPT_HTTPHEADER, $headers); | |
curl_setopt($handle,CURLOPT_RETURNTRANSFER,true); | |
curl_setopt($handle,CURLOPT_SSL_VERIFYHOST,false); | |
curl_setopt($handle,CURLOPT_SSL_VERIFYPEER,false); | |
curl_setopt($handle,CURLOPT_CUSTOMREQUEST,'PUT'); | |
curl_setopt($handle,CURLOPT_POSTFIELDS, $data); | |
$data = curl_exec($handle); | |
curl_close($handle); | |
return $data; | |
} | |
function base64EncodeImage($image_file) { | |
$image_data = fread(fopen($image_file, 'r'), filesize($image_file)); | |
$base64_image = base64_encode($image_data); | |
return $base64_image; | |
} | |
function getImageType($image){ | |
if (function_exists('exif_imagetype')) { | |
return exif_imagetype($image); | |
} | |
try { | |
$info = getimagesize($image); | |
return $info ? $info[2] : false; | |
} catch (\Exception $e) { | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment