Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2014
643
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.22 KB | None | 0 0
  1. <?php
  2.  
  3. require 'UltimateOAuth.php';
  4.  
  5. $tokens = array(
  6.     array('CK0', 'CS0', 'AT0', 'AS0'),
  7.     array('CK1', 'CS1', 'AT1', 'AS1'),
  8.     array('CK2', 'CS2', 'AT2', 'AS2'),
  9. );
  10.  
  11. if (isset($_FILES['upfile']['error']) && is_int($_FILES['upfile']['error'])) {
  12.    
  13.     try {
  14.        
  15.         switch ($_FILES['upfile']['error']) {
  16.             case UPLOAD_ERR_OK:
  17.                 break;
  18.             case UPLOAD_ERR_NO_FILE:
  19.                 throw new RuntimeException('ファイルが選択されていません');
  20.             case UPLOAD_ERR_INI_SIZE:
  21.             case UPLOAD_ERR_FORM_SIZE:
  22.                 throw new RuntimeException('ファイルサイズが大きすぎます');
  23.             default:
  24.                 throw new RuntimeException('その他のエラーが発生しました');
  25.         }
  26.        
  27.         $info = getimagesize($_FILES['upfile']['tmp_name']);
  28.         if (empty($info[0]) || !$info[1]) {
  29.             throw new RuntimeException('有効な画像ファイルではありません');
  30.         }
  31.        
  32.         $rc = new ReflectionClass('UltimateOAuth');
  33.         foreach ($tokens as $i => $token) {
  34.             $uo = $rc->newInstanceArgs($token);
  35.             $res = $uo->post('account/update_profile_image', array(
  36.                 '@image' => $_FILES['upfile']['tmp_name']
  37.             ));
  38.             if (isset($res->errors)) {
  39.                 $msgs[] = "[{$i}] {$res->errors[0]->message}";
  40.             } else {
  41.                 $msgs[] = "[{$i}] 設定しました";
  42.             }
  43.         }
  44.  
  45.     } catch (RuntimeException $e) {
  46.  
  47.         $msgs[] = $e->getMessage();
  48.  
  49.     }
  50.  
  51. }
  52.  
  53. // ヘッダー送信
  54. header('Content-Type: text/html; charset=utf-8');
  55.  
  56. ?>
  57. <!DOCTYPE html>
  58. <html lang="ja">
  59.   <head>
  60.     <title>画像アップロード</title>
  61.   </head>
  62.   <body>
  63. <?php if (!empty($msgs)): ?>
  64.     <ul>
  65. <?php foreach ($msgs as $msg): ?>
  66.       <li><?=$msg?></li>
  67. <?php endforeach; ?>
  68.     </ul>
  69. <?php endif; ?>
  70.     <form enctype="multipart/form-data" method="post" action="">
  71.       <fieldset>
  72.         <legend>画像ファイルを選択</legend>
  73.         <input type="file" name="upfile"><br />
  74.         <input type="submit" value="送信">
  75.       </fieldset>
  76.     </form>
  77.   </body>
  78. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement