最近需要开发一个头像上传的功能,找了很多都需要授权的,后来找到了美图秀秀,功能非常好用。

<?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" 。

转载于:https://www.cnblogs.com/52php/p/5675325.html

PHP流式上传和表单上传(美图秀秀)相关推荐

  1. php结合美图秀秀,美图秀秀web开放平台--PHP流式上传和表单上传示例分享_PHP

    废话少说,直接上代码: 0) { $filename = $save_path . '/' . uniqid() . '.jpg'; $handle = fopen($filename, 'w+'); ...

  2. php文件 用户头像上传代码,网页web上传用户头像代码实现(美图秀秀开放)

    网页web上传用户头像代码实现(美图秀秀开放) 在制作论坛或者一些门户社交网站的时候,经常要获取用户的头像.之前我们一般都是自己制作flash插件头像上传.或者用js来自己开发一个头像上传功能.比如有 ...

  3. 可牛看图web开放平台---PHP表单上传代码分享

    2019独角兽企业重金招聘Python工程师标准>>> 首先打开zendstudio编辑器:直接上代码: ?1234567891011121314151617181920212223 ...

  4. form表单上传文件_SpringBoot中如何使用SpringMVC上传文件?

    今天我们要说的这个话题很简单,不要问为啥,因为SpringBoot,哈哈.现在SpringBoot可以说人人都会用了,它的好处是显而易见的,大大的简化了配置,一起来看看吧. 我们分以下3种情况来谈这个 ...

  5. android multipartentity 怎么上传参数,android-通过MultipartEntityBuilder通过HTTP表单上传文件,并显示进度b...

    android-通过MultipartEntityBuilder通过HTTP表单上传文件,并显示进度b 短版本-.jar已弃用,其升级版本java.lang.NoClassDefFoundError在 ...

  6. serverlet 原理_serverlet实现表单上传文件原理

    ("/home/jh/upload/request.txt"); PrintWriter fileout = new PrintWriter(new FileWriter(f)); ...

  7. Vertx简单表单上传

    我正在使用vertx简单的表单上传.当我上传单个文件时工作正常.如果表单具有"多个"输入并选择多个文件,则HTTPServerUpload出现错误"响应已被写入(java ...

  8. 七牛云上传文件之表单上传文件

    本人愚钝,七牛云上传文件的开发说明文档看了好久才搞懂,才能完成一个实例跑起来.现在做一下总结. 1.注册七牛,新建一个bucket,并获得ak,sk这个不用说了.不涉及到程序编码,重点讲一下编码的流程 ...

  9. php无表单上传文件,php – 如何使用没有实体类的表单上传文件

    我试图使用没有实体类的表单上传文件.到目前为止没有运气. // Controller public function uploadAction() { $request = $this->get ...

最新文章

  1. sphinx error connection to 127.0.0.1:9312 failed (errno=0, msg=)
  2. expect简单教程
  3. 【SICP练习】79 练习2.51
  4. asp.net控件开发基础(20)
  5. 58同城笔试题:数组去重;分饼干(分糖果);最小路径和(leetcode64)
  6. 两种常用的启动和关闭MySQL服务
  7. KB-QA:如何对问题进行信息抽取?
  8. 算法竞赛输入输出!!!
  9. java image 内存不足_一招解决游戏内存不足的神器Caffeine
  10. 在sql语句中该如何处理null值
  11. 高级java面试宝典
  12. 【网络】HTTP原理的简单理解
  13. pyecharts中文手册
  14. 如何将风险应用加入白名单_vivo手机怎么把风险应用添加到白名单
  15. 图像处理之_导数微分
  16. 交换机日志删除_如何查看交换机报警日志 并导出日志 命令是什么
  17. Nginx中的include
  18. 网页设计(四)——DIV+CSS布局3
  19. 西北工业大学附属中学学校简介
  20. P2141珠心算检验

热门文章

  1. 转:(图文并茂)SQL Server 2005详细安装过程及配置
  2. 代码能不能不要写得这么烂?!
  3. 最常被程序员们谎称读过的计算机书籍
  4. 体验VSTS源代码管理之一
  5. golang的reflection(转)(一)
  6. JAVA单向链表实现
  7. 《Node应用程序构建——使用MongoDB和Backbone》一第 1 章 介绍与总览1.1 打造一个社交网络...
  8. Xtrabackup数据全备份与快速搭建从服务器
  9. HIVE 一行转多行输出办法
  10. 常用命令-tar 加密