上传请求代码参考

<?php

echo "<pre>";
print_r(json_decode(uploadFile('Filedata', $_POST), true));die;

function uploadFile($name,$params)
{
    $postField = array();
    $tmpfile = $_FILES[$name]['tmp_name'];
    $filename = basename($_FILES[$name]['name']);
    $postField['files'] = curl_file_create($tmpfile, $_FILES[$name]['type'], $filename);
    $postField = array_merge($postField, $params);
    $headers = array("Content-Type" => "multipart/form-data");
    $curl_handle = curl_init();
    curl_setopt($curl_handle, CURLOPT_URL, 'http://api-img.xxxx.com/upload.php');

    curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl_handle, CURLOPT_POST, TRUE);
    curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $postField);
    curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, TRUE);
    $returned = curl_exec($curl_handle);
    curl_close($curl_handle);
    return $returned;
}

APi代码参考

  <?php

//定义API接口项目根目录
define('IMG_API_PATH', __DIR__);
//定义图片存储根目录
define('IMG_PATH', IMG_API_PATH . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR);

//接受一个项目图片目录
$project_name = isset($_POST['project_name']) && !empty($_POST['project_name']) ? $_POST['project_name'] : '';
if (empty($project_name)) {
    exit(json_encode([
        'status' => 100,
        'message' => 'Please select a project directory!'
    ]));
}

$attachment = isset($_POST['attachment']) && !empty($_POST['attachment']) ? $_POST['attachment'] : '';
if (empty($attachment)) {
    exit(json_encode([
        'status' => 100,
        'message' => 'Please select a attachment directory!'
    ]));
}

//引入composer类库
include './vendor/autoload.php';//https://packagist.org/packages/verot/class.upload.php

use Verot\Upload\Upload;

$handle = new Upload($_FILES['files']);

if ($handle->uploaded) {
    $path_info = pathinfo($attachment);
    if (PHP_OS == 'Linux') {
        $dirname = $path_info['dirname'];
    } else {
        $dirname = PHP_OS == 'Linux' ? $path_info['dirname'] : str_replace('/', '\\', $path_info['dirname']);
    }
    $image_name = $path_info['filename'];
    //生成图片路径
    $server_path = IMG_PATH . $project_name . DIRECTORY_SEPARATOR . 'attachment' . DIRECTORY_SEPARATOR . $dirname . DIRECTORY_SEPARATOR;
    //生成唯一文件名称
    $handle->file_new_name_body = $image_name;
    $handle->image_ratio = true;

    //Set Image watermark
    $handle->image_watermark = IMG_PATH . $project_name . DIRECTORY_SEPARATOR . 'watermark.png';
    $handle->image_watermark_position = 'BR';

    $handle->process($server_path);
    if ($handle->processed) {
        $file_dst_pathname = explode('..', $handle->file_dst_pathname);
        if (isset($file_dst_pathname[1])) {
            $attachment = str_replace(DIRECTORY_SEPARATOR . $project_name, '', $file_dst_pathname[1]);
        } else {
            $attachment = $file_dst_pathname;
        }
        $data = [
            'status' => 0,
            'message' => 'success',
            'ext' => $handle->file_dst_name_ext,
            'maxsize' => $handle->file_max_size,
            'image' => $handle->file_is_image,
            'size' => $handle->file_src_size,
            'type' => $handle->file_src_mime,
            'attachment' => $attachment
        ];
        $handle->clean();
    } else {
        $data = [
            'status' => 100,
            'message' => $handle->error
        ];
    }
    exit(json_encode($data));
}

关注友儿不迷路

Last modification:August 16th, 2021 at 11:02 am
如果觉得我的文章对你有用,请随意赞赏