为什么我得到"为foreach()提供的参数无效"在这个PHP代码?

时间:2018-03-14 18:54:36

标签: php google-drive-api google-drive-realtime-api

我知道已经有成千上万的主题出现此错误,但我给了一个阅读,我真的不明白为什么会出现这个错误。我无法下载我想要的文件。

因为它是以下,我只有主文件夹的id,我需要让驱动器读取所有路径并打开并选择文件X.你几乎完成了代码,但它开始给出错误。

function retrieveAllFiles($service) {
      global $i;
      global $pieces;
      if (isset($i)){
        echo $i;
      }else{
        echo 'error #1';
      }
  $result = array();
  $pageToken = NULL;

  do {
    try {      
      $folderId = 'folder id here';
      $parameters = array();
      $parameters['q'] = "parents in '".$folderId."'";
      if ($pageToken) {
        $parameters['pageToken'] = $pageToken;
      }

      $files = $service->files->listFiles($parameters);
      $result = array_merge($result, $files->getItems());
      $pageToken = $files->getNextPageToken();
    } catch (Exception $e) {
      print "An error occurred: " . $e->getMessage();
      $pageToken = NULL;
    }
    $continuacao = Downlaod();
  } while ($pageToken);
  return $result;

}

$service=buildService();
$result=retrieveAllFiles($service);
$caminho  = "WHATEVER:/MEPROMIN/VLP/CSK/CANTIL/Write 1";
$pieces = explode("/", $caminho);
$ultimo = end($pieces); // LAST VALUE
$array = count($pieces); // LAST ARRAY POSITION

function Downlaod() {
  global $result;
  global $pieces;
  global $ultimo;
  global $array;
  global $service;
  global $i;
foreach ($result as $value) {
  if ($pieces[1] == $value->title or $pieces[2] == $value->title){
      if (empty($i)){
      $i = 1;
      }
      echo $pieces[$i];
      while ($i < $array-1){

        if ($pieces[$i] == $value->title){
          $folderId = $value->alternateLink;
          if ($i != $array-1){     
            $i = $i + 1;
            $continue=retrieveAllFiles($service);
          }
          if ($i == $array-1){

                          echo "teste";
                          function downloadFile($service, $file) {
                          $downloadUrl = $file->getDownloadUrl();
                          if ($downloadUrl) {
                          $request = new Google_HttpRequest($downloadUrl, 'GET', null, null);
                          $httpRequest = Google_Client::$io->authenticatedRequest($request);
                          if ($httpRequest->getResponseHttpCode() == 200) {

                          return $httpRequest->getResponseBody();
                          } else {
                          // An error occurred.
                          return null;
                          }
                          } else {
                          // The file doesn't have any content stored on Drive.
                          return null;
                          }
                          }
                          try {
                          $file_id='the id file here';

                          $service=buildService();
                          //printFile($service,$file_id);
                          $file = $service->files->get($file_id);
                          header('Content-Type: '.$file->getMimeType());
                          header('Content-Disposition: attachment; filename='.'my_file.zip');
                          print(downloadFile($service,$file));


                            } catch (Exception $e) {
                            print "An error occurred1: " . $e->getMessage();
                            }
          }
          $i++;  
        }else
          echo "Error #2";
          $i++;
          break;
        }
  }else{
    echo "File not found."."<br>";
  }




    }
  # code...

  }
$continuacao = Downlaod()

0 个答案:

没有答案