質問編集履歴

2

本文修正

2020/07/06 10:23

投稿

study_111
study_111

スコア82

test CHANGED
File without changes
test CHANGED
@@ -48,7 +48,7 @@
48
48
 
49
49
 
50
50
 
51
- 以下は、upload.php
51
+ 以下は、upload.phpになりま
52
52
 
53
53
  ```php
54
54
 

1

コード変更、追記

2020/07/06 10:23

投稿

study_111
study_111

スコア82

test CHANGED
File without changes
test CHANGED
@@ -48,7 +48,7 @@
48
48
 
49
49
 
50
50
 
51
- コードの全文は以下になりま
51
+ 以下は、upload.phpで
52
52
 
53
53
  ```php
54
54
 
@@ -186,426 +186,396 @@
186
186
 
187
187
 
188
188
 
189
- // ファイルがあれば処理実行
190
-
191
- if(isset($_FILES["upload_file"])){
192
-
193
-
194
-
195
- // アップロードされたファイル件を処理
196
-
197
- for($i = 0; $i < count($_FILES["upload_file"]["name"]); $i++ ){
198
-
199
-
200
-
201
- // アップロードされたファイルか検査
202
-
203
- if(is_uploaded_file($_FILES["upload_file"]["tmp_name"][$i])){
189
+ if(isset($_FILES['certification_file']['tmp_name'])){
190
+
204
-
191
+ // echo('jsonファイルは存在します');
192
+
205
-
193
+ // echo($_FILES['certification_file']['tmp_name']);
194
+
206
-
195
+ $ext = getExt($_FILES['certification_file']['name']);
196
+
207
- // ファイルをお好みの場所に移動
197
+ // echo(__LINE__);
198
+
208
-
199
+ echo($ext);
200
+
201
+
202
+
203
+ if ($ext == "json"){
204
+
205
+ // echo("jsonファイルです。");
206
+
207
+ $certification_tmpfile = $_FILES['certification_file']['tmp_name'];
208
+
209
+ $certification_filename = "./credentials/" . date("YmdHis");
210
+
211
+ $certification_filename .= mt_rand();
212
+
209
- move_uploaded_file($_FILES["upload_file"]["tmp_name"][$i], "./images/" . $_FILES["upload_file"]["name"][$i]);
213
+ // $certification_filename = "./credentials/". $_FILES['certification_file']['name'];
214
+
215
+ $certification_filename .= '.' . $ext;
216
+
217
+ if (is_uploaded_file($certification_tmpfile)) {
218
+
219
+
220
+
221
+ if ( move_uploaded_file($certification_tmpfile , $certification_filename)) {
222
+
223
+ echo $certification_filename . "をアップロードしました。";
224
+
225
+ } else {
226
+
227
+ echo "ファイルをアップロードできません。";
228
+
229
+ }
230
+
231
+
232
+
233
+ } else {
234
+
235
+ echo "ファイルが選択されていません。";
210
236
 
211
237
  }
212
238
 
239
+ }else{
240
+
241
+ echo("credentials.jsonファイルではありません。");
242
+
213
243
  }
214
244
 
245
+
246
+
247
+ // echo($_FILES['certification_file']['type']);
248
+
249
+ // echo($certification_filename);
250
+
251
+ // echo "\n";
252
+
253
+
254
+
255
+ // echo($certification_tmpfile);
256
+
215
- }
257
+ }
216
-
217
-
218
-
258
+
259
+
260
+
219
- if(isset($_FILES['certification_file']['tmp_name'])){
261
+ if(isset($_FILES['movie_file']['tmp_name'])){
220
-
221
- // echo('jsonファイルは存在します');
262
+
222
-
223
- // echo($_FILES['certification_file']['tmp_name']);
263
+
224
-
264
+
225
- $ext = getExt($_FILES['certification_file']['name']);
265
+ $ext = getExt($_FILES['movie_file']['name']);
226
-
227
- // echo(__LINE__);
266
+
228
-
229
- echo($ext);
267
+
230
-
231
-
232
-
233
- if ($ext == "json"){
268
+
234
-
235
- // echo("jsonファイルです。");
236
-
237
- $certification_tmpfile = $_FILES['certification_file']['tmp_name'];
269
+ $movie_tmpfile = $_FILES['movie_file']['tmp_name'];
238
-
270
+
271
+
272
+
239
- $certification_filename = "./credentials/" . date("YmdHis");
273
+ $movie_filename = "./movies/" . date("YmdHis");
240
-
241
- $certification_filename .= mt_rand();
274
+
242
-
243
- // $certification_filename = "./credentials/". $_FILES['certification_file']['name'];
244
-
245
- $certification_filename .= '.' . $ext;
275
+ $movie_filename .= "." . $ext;
246
-
276
+
277
+
278
+
247
- if (is_uploaded_file($certification_tmpfile)) {
279
+ if (is_uploaded_file($movie_tmpfile)) {
248
-
249
-
250
-
280
+
281
+
282
+
251
- if ( move_uploaded_file($certification_tmpfile , $certification_filename)) {
283
+ if ( move_uploaded_file($movie_tmpfile , $movie_filename)) {
252
-
284
+
253
- echo $certification_filename . "をアップロードしました。";
285
+ echo $movie_filename . "をアップロードしました。";
254
-
255
- } else {
256
-
257
- echo "ファイルをアップロードできません。";
258
-
259
- }
260
-
261
-
262
286
 
263
287
  } else {
264
288
 
265
- echo "ファイルが選択されていません。";
289
+ echo "ファイルをアップロードできません。";
266
290
 
267
291
  }
268
292
 
269
- }else{
293
+ } else {
270
-
294
+
271
- echo("credentials.jsonファイルではありません。");
295
+ echo "ファイルが選択されていません。";
272
296
 
273
297
  }
274
298
 
275
299
 
276
300
 
277
- // echo($_FILES['certification_file']['type']);
278
-
279
- // echo($certification_filename);
280
-
281
- // echo "\n";
282
-
283
-
284
-
285
- // echo($certification_tmpfile);
286
-
287
- }
288
-
289
-
290
-
291
- if(isset($_FILES['movie_file']['tmp_name'])){
292
-
293
-
294
-
295
- $ext = getExt($_FILES['movie_file']['name']);
296
-
297
-
298
-
299
- $movie_tmpfile = $_FILES['movie_file']['tmp_name'];
300
-
301
-
302
-
303
- $movie_filename = "./movies/" . date("YmdHis");
304
-
305
- $movie_filename .= "." . $ext;
306
-
307
-
308
-
309
- if (is_uploaded_file($movie_tmpfile)) {
310
-
311
-
312
-
313
- if ( move_uploaded_file($movie_tmpfile , $movie_filename)) {
314
-
315
- echo $movie_filename . "をアップロードしました。";
316
-
317
- } else {
318
-
319
- echo "ファイルをアップロードできません。";
320
-
321
- }
322
-
323
- } else {
324
-
325
- echo "ファイルが選択されていません。";
301
+ // echo($movie_tmpfile);
302
+
303
+ }
304
+
305
+
306
+
307
+ // echo($movie_filename)."\n";
308
+
309
+
310
+
311
+
312
+
313
+
314
+
315
+ $url = $certification_filename;
316
+
317
+ $json = file_get_contents($url);
318
+
319
+ $json = mb_convert_encoding($json, 'UTF8', 'ASCII,JIS,UTF-8,EUC-JP,SJIS-WIN');
320
+
321
+ // echo($json);
322
+
323
+ $cert_arr = json_decode($json,true);
324
+
325
+ // var_dump($cert_arr);
326
+
327
+
328
+
329
+
330
+
331
+ if (!file_exists(__DIR__ . '/vendor/autoload.php')) {
332
+
333
+ throw new \Exception('please run "composer require google/apiclient:~2.0" in "' . __DIR__ .'"');
334
+
335
+ }
336
+
337
+
338
+
339
+ $OAUTH2_CLIENT_ID = $cert_arr['web']['client_id'];
340
+
341
+ $OAUTH2_CLIENT_SECRET = $cert_arr['web']['client_secret'];
342
+
343
+ // echo($OAUTH2_CLIENT_ID);
344
+
345
+ echo '<br>';
346
+
347
+ // echo($OAUTH2_CLIENT_SECRET);
348
+
349
+ echo($_SERVER['HTTP_HOST'] );
350
+
351
+ $client = new Google_Client();
352
+
353
+ $client->setClientId($OAUTH2_CLIENT_ID);
354
+
355
+ $client->setClientSecret($OAUTH2_CLIENT_SECRET);
356
+
357
+ $client->setScopes('https://www.googleapis.com/auth/youtube');
358
+
359
+ $redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],
360
+
361
+ FILTER_SANITIZE_URL);
362
+
363
+ $client->setRedirectUri($redirect);
364
+
365
+
366
+
367
+ // 使用するサービスを作成
368
+
369
+ $youtube = new Google_Service_YouTube($client);
370
+
371
+
372
+
373
+ // var_dump($youtube);
374
+
375
+ $tokenSessionKey = 'token-' . $client->prepareScopes();
376
+
377
+ if (isset($_GET['code'])) {
378
+
379
+ if (strval($_SESSION['state']) !== strval($_GET['state'])) {
380
+
381
+ die('The session state did not match.');
326
382
 
327
383
  }
328
384
 
329
-
330
-
331
- // echo($movie_tmpfile);
332
-
333
- }
334
-
335
-
336
-
337
- // echo($movie_filename)."\n";
338
-
339
-
340
-
341
-
342
-
343
-
344
-
345
- $url = $certification_filename;
346
-
347
- $json = file_get_contents($url);
348
-
349
- $json = mb_convert_encoding($json, 'UTF8', 'ASCII,JIS,UTF-8,EUC-JP,SJIS-WIN');
350
-
351
- // echo($json);
352
-
353
- $cert_arr = json_decode($json,true);
354
-
355
- // var_dump($cert_arr);
356
-
357
-
358
-
359
-
360
-
361
- if (!file_exists(__DIR__ . '/vendor/autoload.php')) {
362
-
363
- throw new \Exception('please run "composer require google/apiclient:~2.0" in "' . __DIR__ .'"');
364
-
365
- }
366
-
367
-
368
-
369
- $OAUTH2_CLIENT_ID = $cert_arr['web']['client_id'];
370
-
371
- $OAUTH2_CLIENT_SECRET = $cert_arr['web']['client_secret'];
372
-
373
- // echo($OAUTH2_CLIENT_ID);
374
-
375
- echo '<br>';
376
-
377
- // echo($OAUTH2_CLIENT_SECRET);
378
-
379
- echo($_SERVER['HTTP_HOST'] );
380
-
381
- $client = new Google_Client();
382
-
383
- $client->setClientId($OAUTH2_CLIENT_ID);
384
-
385
- $client->setClientSecret($OAUTH2_CLIENT_SECRET);
386
-
387
- $client->setScopes('https://www.googleapis.com/auth/youtube');
388
-
389
- $redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],
390
-
391
- FILTER_SANITIZE_URL);
392
-
393
- $client->setRedirectUri($redirect);
394
-
395
-
396
-
397
- // 使用するサービスを作成
398
-
399
- $youtube = new Google_Service_YouTube($client);
400
-
401
-
402
-
403
- // var_dump($youtube);
404
-
405
- $tokenSessionKey = 'token-' . $client->prepareScopes();
406
-
407
- if (isset($_GET['code'])) {
408
-
409
- if (strval($_SESSION['state']) !== strval($_GET['state'])) {
410
-
411
- die('The session state did not match.');
385
+ $client->authenticate($_GET['code']);
386
+
387
+ $_SESSION[$tokenSessionKey] = $client->getAccessToken();
388
+
389
+ header('Location: ' . $redirect);
390
+
391
+ }
392
+
393
+ if (isset($_SESSION[$tokenSessionKey])) {
394
+
395
+ $client->setAccessToken($_SESSION[$tokenSessionKey]);
396
+
397
+ }
398
+
399
+
400
+
401
+
402
+
403
+ // Check to ensure that the access token was successfully acquired.
404
+
405
+ if ($client->getAccessToken()) {
406
+
407
+ $htmlBody = '';
408
+
409
+ try{
410
+
411
+
412
+
413
+ //アップロードするファイルパスへ置き換える
414
+
415
+ $videoPath = $movie_filename;
416
+
417
+
418
+
419
+ // Create a snippet with title, description, tags and category ID
420
+
421
+ // Create an asset resource and set its snippet metadata and type.
422
+
423
+ // This example sets the video's title, description, keyword tags, and
424
+
425
+ // video category.
426
+
427
+
428
+
429
+ $snippet = new Google_Service_YouTube_VideoSnippet();
430
+
431
+ $snippet->setTitle($title);
432
+
433
+ $snippet->setDescription($description);
434
+
435
+ $snippet->setTags($tag_array);
436
+
437
+
438
+
439
+ // Numeric video category. See
440
+
441
+ // https://developers.google.com/youtube/v3/docs/videoCategories/list
442
+
443
+ $snippet->setCategoryId($category_id);
444
+
445
+
446
+
447
+ // Set the video's status to "public". Valid statuses are "public",
448
+
449
+ // "private" and "unlisted".
450
+
451
+ $status = new Google_Service_YouTube_VideoStatus();
452
+
453
+
454
+
455
+ //公開ステータスは「非公開」に設定
456
+
457
+ $status->privacyStatus = "private";
458
+
459
+
460
+
461
+ // Associate the snippet and status objects with a new video resource.
462
+
463
+ $video = new Google_Service_YouTube_Video();
464
+
465
+ $video->setSnippet($snippet);
466
+
467
+ $video->setStatus($status);
468
+
469
+
470
+
471
+ // Specify the size of each chunk of data, in bytes. Set a higher value for
472
+
473
+ // reliable connection as fewer chunks lead to faster uploads. Set a lower
474
+
475
+ // value for better recovery on less reliable connections.
476
+
477
+ $chunkSizeBytes = 1 * 1024 * 1024;
478
+
479
+
480
+
481
+ // Setting the defer flag to true tells the client to return a request which can be called
482
+
483
+ // with ->execute(); instead of making the API call immediately.
484
+
485
+ $client->setDefer(true);
486
+
487
+
488
+
489
+ // Create a request for the API's videos.insert method to create and upload the video.
490
+
491
+ $insertRequest = $youtube->videos->insert("status,snippet", $video);
492
+
493
+
494
+
495
+ // Create a MediaFileUpload object for resumable uploads.
496
+
497
+ $media = new Google_Http_MediaFileUpload(
498
+
499
+ $client,
500
+
501
+ $insertRequest,
502
+
503
+ 'video/*',
504
+
505
+ null,
506
+
507
+ true,
508
+
509
+ $chunkSizeBytes
510
+
511
+ );
512
+
513
+ $media->setFileSize(filesize($videoPath));
514
+
515
+
516
+
517
+
518
+
519
+ // Read the media file and upload it chunk by chunk.
520
+
521
+ $status = false;
522
+
523
+ $handle = fopen($videoPath, "rb");
524
+
525
+ while (!$status && !feof($handle)) {
526
+
527
+ $chunk = fread($handle, $chunkSizeBytes);
528
+
529
+ $status = $media->nextChunk($chunk);
530
+
531
+ }
532
+
533
+
534
+
535
+ fclose($handle);
536
+
537
+
538
+
539
+ // If you want to make other calls after the file upload, set setDefer back to false
540
+
541
+ $client->setDefer(false);
542
+
543
+
544
+
545
+
546
+
547
+ $htmlBody .= "<h3>Video Uploaded</h3><ul>";
548
+
549
+ $htmlBody .= sprintf('<li>%s (%s)</li>',
550
+
551
+ $status['snippet']['title'],
552
+
553
+ $status['id']);
554
+
555
+
556
+
557
+ $htmlBody .= '</ul>';
558
+
559
+
560
+
561
+ } catch (Google_Service_Exception $e) {
562
+
563
+ $htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>',
564
+
565
+ htmlspecialchars($e->getMessage()));
566
+
567
+ } catch (Google_Exception $e) {
568
+
569
+ $htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>',
570
+
571
+ htmlspecialchars($e->getMessage()));
412
572
 
413
573
  }
414
574
 
415
- $client->authenticate($_GET['code']);
575
+
416
576
 
417
577
  $_SESSION[$tokenSessionKey] = $client->getAccessToken();
418
578
 
419
- header('Location: ' . $redirect);
420
-
421
- }
422
-
423
- if (isset($_SESSION[$tokenSessionKey])) {
424
-
425
- $client->setAccessToken($_SESSION[$tokenSessionKey]);
426
-
427
- }
428
-
429
-
430
-
431
-
432
-
433
- // Check to ensure that the access token was successfully acquired.
434
-
435
- if ($client->getAccessToken()) {
436
-
437
- $htmlBody = '';
438
-
439
- try{
440
-
441
-
442
-
443
- //アップロードするファイルパスへ置き換える
444
-
445
- $videoPath = $movie_filename;
446
-
447
-
448
-
449
- // Create a snippet with title, description, tags and category ID
450
-
451
- // Create an asset resource and set its snippet metadata and type.
452
-
453
- // This example sets the video's title, description, keyword tags, and
454
-
455
- // video category.
456
-
457
-
458
-
459
- $snippet = new Google_Service_YouTube_VideoSnippet();
460
-
461
- $snippet->setTitle($title);
462
-
463
- $snippet->setDescription($description);
464
-
465
- $snippet->setTags($tag_array);
466
-
467
-
468
-
469
- // Numeric video category. See
470
-
471
- // https://developers.google.com/youtube/v3/docs/videoCategories/list
472
-
473
- $snippet->setCategoryId($category_id);
474
-
475
-
476
-
477
- // Set the video's status to "public". Valid statuses are "public",
478
-
479
- // "private" and "unlisted".
480
-
481
- $status = new Google_Service_YouTube_VideoStatus();
482
-
483
-
484
-
485
- //公開ステータスは「非公開」に設定
486
-
487
- $status->privacyStatus = "private";
488
-
489
-
490
-
491
- // Associate the snippet and status objects with a new video resource.
492
-
493
- $video = new Google_Service_YouTube_Video();
494
-
495
- $video->setSnippet($snippet);
496
-
497
- $video->setStatus($status);
498
-
499
-
500
-
501
- // Specify the size of each chunk of data, in bytes. Set a higher value for
502
-
503
- // reliable connection as fewer chunks lead to faster uploads. Set a lower
504
-
505
- // value for better recovery on less reliable connections.
506
-
507
- $chunkSizeBytes = 1 * 1024 * 1024;
508
-
509
-
510
-
511
- // Setting the defer flag to true tells the client to return a request which can be called
512
-
513
- // with ->execute(); instead of making the API call immediately.
514
-
515
- $client->setDefer(true);
516
-
517
-
518
-
519
- // Create a request for the API's videos.insert method to create and upload the video.
520
-
521
- $insertRequest = $youtube->videos->insert("status,snippet", $video);
522
-
523
-
524
-
525
- // Create a MediaFileUpload object for resumable uploads.
526
-
527
- $media = new Google_Http_MediaFileUpload(
528
-
529
- $client,
530
-
531
- $insertRequest,
532
-
533
- 'video/*',
534
-
535
- null,
536
-
537
- true,
538
-
539
- $chunkSizeBytes
540
-
541
- );
542
-
543
- $media->setFileSize(filesize($videoPath));
544
-
545
-
546
-
547
-
548
-
549
- // Read the media file and upload it chunk by chunk.
550
-
551
- $status = false;
552
-
553
- $handle = fopen($videoPath, "rb");
554
-
555
- while (!$status && !feof($handle)) {
556
-
557
- $chunk = fread($handle, $chunkSizeBytes);
558
-
559
- $status = $media->nextChunk($chunk);
560
-
561
- }
562
-
563
-
564
-
565
- fclose($handle);
566
-
567
-
568
-
569
- // If you want to make other calls after the file upload, set setDefer back to false
570
-
571
- $client->setDefer(false);
572
-
573
-
574
-
575
-
576
-
577
- $htmlBody .= "<h3>Video Uploaded</h3><ul>";
578
-
579
- $htmlBody .= sprintf('<li>%s (%s)</li>',
580
-
581
- $status['snippet']['title'],
582
-
583
- $status['id']);
584
-
585
-
586
-
587
- $htmlBody .= '</ul>';
588
-
589
-
590
-
591
- } catch (Google_Service_Exception $e) {
592
-
593
- $htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>',
594
-
595
- htmlspecialchars($e->getMessage()));
596
-
597
- } catch (Google_Exception $e) {
598
-
599
- $htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>',
600
-
601
- htmlspecialchars($e->getMessage()));
602
-
603
- }
604
-
605
-
606
-
607
- $_SESSION[$tokenSessionKey] = $client->getAccessToken();
608
-
609
579
  } elseif ($OAUTH2_CLIENT_ID == 'REPLACE_ME') {
610
580
 
611
581
  $htmlBody = <<<END
@@ -654,8 +624,22 @@
654
624
 
655
625
 
656
626
 
627
+
628
+
657
629
  ### 試したこと
658
630
 
659
631
 
660
632
 
661
633
  エラー内容をインターネットで検索するも、改善に至る記事が見当たらない状況です。
634
+
635
+
636
+
637
+ ### 追記
638
+
639
+ htmlファイルのformから値を入力し、POSTリクエストを行なった結果、以下のエラーメッセージが出力されました。
640
+
641
+ ```
642
+
643
+ Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting '-' or identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in /var/www/html/upload.php on line 275
644
+
645
+ ```