美图秀秀web开放平台--PHP流式上传和表单上传示例分享
废话少说,直接上代码:
<?php /** *Note:foroctet-streamupload *这个是流式上传PHP文件 *Pleasebeamendedaccordinglybasedontheactualsituation */ $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'Imagedatasavesuccessed,file:'.$filename; exit(); }else{ die('Imageuploaderror!'); } }else{ die('Imagedatanotdetected!'); }
<?php /** *Note:formultipart/form-dataupload *这个是标准表单上传PHP文件 *Pleasebeamendedaccordinglybasedontheactualsituation */ if(!$_FILES['Filedata']){ die('Imagedatanotdetected!'); } if($_FILES['Filedata']['error']>0){ switch($_FILES['Filedata']['error']){ case1: $error_log='ThefileisbiggerthanthisPHPinstallationallows'; break; case2: $error_log='Thefileisbiggerthanthisformallows'; break; case3: $error_log='Onlypartofthefilewasuploaded'; break; case4: $error_log='Nofilewasuploaded'; break; default: break; } die('uploaderror:'.$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='onlyallowjpg,png,gif'; die('uploaderror:'.$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('Imageuploaderror!'); } //图片保存路径,默认保存在该代码所在目录(可根据实际需求修改保存路径) $save_path=dirname(__FILE__); $uinqid=uniqid(); $filename=$save_path.'/'.$uinqid.'.'.$extension; $result=move_uploaded_file($img_data,$filename); if(!$result||!is_file($filename)){ die('Imageuploaderror!'); } echo'Imagedatasavesuccessed,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"。