第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 Google Drive API v3 和 php 通過 html 表單上傳文件

使用 Google Drive API v3 和 php 通過 html 表單上傳文件

PHP
慕運維8079593 2023-03-26 14:10:59
我正在開發(fā)一個程序,允許用戶使用 HTML 表單選擇一個文件,然后該程序會將所述文件上傳到 Google Drive 并與某個其他用戶共享。我已經(jīng)能夠在 Google Drive 中創(chuàng)建一個文件然后共享它;但是一旦我嘗試上傳一個文件,它就開始走下坡路了。這很奇怪,因為我認為上傳文件的代碼是正確的,所以問題一定在于我如何將文件從 HTML 表單獲取到 PHP 文件。我的懷疑有點被我試圖 echo $_FILES['myfile']['name'] 沒有任何反應這一事實所證實。我將在下面上傳兩個代碼片段,但為了簡潔起見,我將省略所有 Google Drive api 身份驗證,因為它有效。索引.php      <form action="../Next.php" method="POST" enctype="multipart/form-data">    <section class="file">      <span>Please select the file to be uploaded </span>      <br>      <label for="myfile">Select a file:</label>      <input type="file"  name="myfile" required  >     </section>    <section class="submission">      <input type="submit" value="Submit">    </section>  </form>下一步.php    $myfile=$_FILES['myfile']['tmp_name'];    $type=mime_content_type($myfile);    $drive = new Google_Service_Drive($client);    $file = new Google_Service_Drive_DriveFile();    $file->setTitle('Test');    $file->setDescription('Test');    $file->setMimeType($type);    $data = file_get_contents($myfile);    $uploadedFile = $service->files->insert($file, array(      'data' => $data,      'mimeType' => $type,     ));請記住我省略了驗證碼,因為我知道它有效。我不明白為什么這什么都不做,從我在互聯(lián)網(wǎng)上搜集到的代碼是正確的。我認為這個問題可能與 $_FILES['myfile'] 數(shù)組有關,但從所有在線文檔來看,它似乎也是正確的。
查看完整描述

3 回答

?
慕森卡

TA貢獻1806條經(jīng)驗 獲得超8個贊

如果仍未解決,那么...正如您提到的 $_FILES 為空,那么我建議查看項目中的文件位置。假設您index.phpPROJECT->HOME->index.php目錄中,那么您Next.php應該在PROJECT->Next.php目錄中。

你也必須給提交一個name=submit。

Next.php測試中是否設置了任何一種形式

if(isset($_POST['submit'])){ print_r($_FILES['myfile']); }

如果沒有上傳文件,請告訴我問題所在。


查看完整回答
反對 回復 2023-03-26
?
一只名叫tom的貓

TA貢獻1906條經(jīng)驗 獲得超3個贊

初始設置

由于您的 HTML 文件規(guī)范,我假設您的項目結構如下:


project/

├── templates/

│   └── index.html

└── Next.php

因此,我將使用以下命令在項目文件夾中啟動一個 php 簡單的 http 服務器:php -S localhost:8000


方法

為了讓它工作,你將不得不Next.php稍微調(diào)整你的代碼。我建議使用最新的 Drive APi 版本 (v3)。v3 中沒有方法insert(),但有create().


// Get the API client and construct the service object.

$client = getClient();

$service = new Google_Service_Drive($client);

$myfile=$_FILES['myfile']['tmp_name'];


$fileMetadata = new Google_Service_Drive_DriveFile(array(

    'name' => "Test",

));


try{


    $newFile = $service->files->create(

        $fileMetadata,

        array(

            'data' => file_get_contents($myfile),

            'mimeType' => mime_content_type($myfile),

            'uploadType' => 'multipart'

        )

    );



} catch(Exception $e){


    echo 'An error ocurred : ' . $e->getMessage();


}


查看完整回答
反對 回復 2023-03-26
?
慕蓋茨4494581

TA貢獻1850條經(jīng)驗 獲得超11個贊

嘗試添加多部分


$uploadedFile = $service->files->insert($file, array(

  'data' => $data,

  'mimeType' => $type,

  'uploadType' => 'multipart'

));


查看完整回答
反對 回復 2023-03-26
  • 3 回答
  • 0 關注
  • 147 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號