|
廢話少說,直接上代碼:
<?php/** * Note:for octet-stream upload * 這個(gè)是流式上傳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 * 這個(gè)是標(biāo)準(zhǔn)表單上傳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!');}// 圖片保存路徑,默認(rèn)保存在該代碼所在目錄(可根據(jù)實(shí)際需求修改保存路徑)$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 ();
備注:美圖秀秀提供兩個(gè)上傳接口供測試
一個(gè)是octet-stream方式上傳,地址為:http://imgkaka.meitu.com/xiuxiu_web_pic_save.php
另一個(gè)是multipart/form-data方式上傳,地址為:http://web.upload.meitu.com/image_upload.php
表單名稱為"upload_file"。
php技術(shù):美圖秀秀web開放平臺(tái)--PHP流式上傳和表單上傳示例分享,轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。