美图秀秀web开放平台--PHP流式上传和表单上传示例分享

作者:hebedich 时间:2023-11-15 08:34:42 

废话少说,直接上代码:


<?php
/**
* Note:for octet-stream upload
* 这个是流式上传PHP文件
* Please be amended accordingly based on the actual situation
*/
$post_input = 'php://input';
$save_path = dirname(__FILE__);
$postdata = file_get_contents($post_input);
if (isset($postdata) && strlen($postdata) > 0) {
$filename = $save_path . '/' . uniqid() . '.jpg';
$handle = fopen($filename, 'w+');
fwrite($handle, $postdata);
fclose($handle);
if (is_file($filename)) {
 echo 'Image data save successed,file:' . $filename;
 exit ();
} else {
 die ('Image upload error!');
}
} else {
die ('Image data not detected!');
}

<?php
/**
* Note:for multipart/form-data upload
* 这个是标准表单上传PHP文件
* Please be amended accordingly based on the actual situation
*/
if (!$_FILES['Filedata']) {
die ('Image data not detected!');
}
if ($_FILES['Filedata']['error'] > 0) {
switch ($_FILES ['Filedata'] ['error']) {
 case 1 :
  $error_log = 'The file is bigger than this PHP installation allows';
  break;
 case 2 :
  $error_log = 'The file is bigger than this form allows';
  break;
 case 3 :
  $error_log = 'Only part of the file was uploaded';
  break;
 case 4 :
  $error_log = 'No file was uploaded';
  break;
 default :
  break;
}
die ('upload error:' . $error_log);
} else {
$img_data = $_FILES['Filedata']['tmp_name'];
$size = getimagesize($img_data);
$file_type = $size['mime'];
if (!in_array($file_type, array('image/jpg', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/gif'))) {
 $error_log = 'only allow jpg,png,gif';
 die ('upload error:' . $error_log);
}
switch ($file_type) {
 case 'image/jpg' :
 case 'image/jpeg' :
 case 'image/pjpeg' :
  $extension = 'jpg';
  break;
 case 'image/png' :
  $extension = 'png';
  break;
 case 'image/gif' :
  $extension = 'gif';
  break;
}
}

if (!is_file($img_data)) {
die ('Image upload error!');
}

// 图片保存路径,默认保存在该代码所在目录(可根据实际需求修改保存路径)
$save_path = dirname(__FILE__);
$uinqid = uniqid();
$filename = $save_path . '/' . $uinqid . '.' . $extension;
$result = move_uploaded_file($img_data, $filename);
if (!$result || !is_file($filename)) {
die ('Image upload error!');
}
echo 'Image data save successed,file:' . $filename;
exit ();

备注:美图秀秀提供两个上传接口供测试
一个是octet-stream方式上传,地址为:http://imgkaka.meitu.com/xiuxiu_web_pic_save.php
另一个是multipart/form-data方式上传,地址为:http://web.upload.meitu.com/image_upload.php
表单名称为"upload_file"。

标签:流式上传,表单上传
0
投稿

猜你喜欢

  • 利用 python 对目录下的文件进行过滤删除

    2022-07-01 01:24:32
  • 如何在Python中进行异常处理

    2021-02-21 06:51:01
  • 配置 SQL Server 2005 以允许远程连接的方法

    2024-01-13 12:58:40
  • 浅述python中深浅拷贝原理

    2022-03-05 06:29:36
  • django queryset相加和筛选教程

    2022-09-10 11:04:43
  • python 如何将数据写入本地txt文本文件的实现方法

    2021-05-02 14:11:52
  • python中not not x 与bool(x) 的区别

    2021-04-27 03:50:17
  • 22个HTML5的初级技巧

    2010-12-17 12:39:00
  • Sqlserver 2000/2005/2008 的收缩日志方法和清理日志方法

    2012-07-21 14:55:18
  • Pandas中时间序列的处理大全

    2023-08-14 06:18:30
  • 网站制作流程及界面交互设计

    2007-10-26 12:00:00
  • MySQL 8.0.19支持输入3次错误密码锁定账户功能(例子)

    2024-01-28 04:48:00
  • python爬取微信公众号文章

    2021-12-30 18:46:35
  • 一步步教你用Python实现2048小游戏

    2023-02-15 20:51:29
  • python中三种输出格式总结(%,format,f-string)

    2022-06-11 16:34:40
  • MediaPipe API实现骨骼识别功能分步讲解流程

    2022-06-17 05:41:10
  • Python参数传递对象的引用原理解析

    2023-04-23 18:33:57
  • pandas的排序、分组groupby及cumsum累计求和方式

    2023-07-20 07:00:39
  • 在Django的session中使用User对象的方法

    2022-04-23 01:20:07
  • OpenLayer基于vue的封装使用教程

    2024-05-11 09:16:22
  • asp之家 网络编程 m.aspxhome.com