Skip to content

Commit 523430f

Browse files
committed
Merge pull request googleapis#548 from woofyman99/master
More efficient file upload from URL
2 parents ef2b8f6 + a73b5b2 commit 523430f

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

examples/fileupload.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@
9595
$status = false;
9696
$handle = fopen(TESTFILE, "rb");
9797
while (!$status && !feof($handle)) {
98-
$chunk = fread($handle, $chunkSizeBytes);
98+
// read until you get $chunkSizeBytes from TESTFILE
99+
// fread will never return more than 8192 bytes if the stream is read buffered and it does not represent a plain file
100+
// An example of a read buffered file is when reading from a URL
101+
$chunk = readVideoChunk($handle, $chunkSizeBytes);
99102
$status = $media->nextChunk($chunk);
100103
}
101104

@@ -113,18 +116,34 @@
113116
echo missingClientSecretsWarning();
114117
exit;
115118
}
119+
function readVideoChunk ($handle, $chunkSize)
120+
{
121+
$byteCount = 0;
122+
$giantChunk = "";
123+
while (!feof($handle)) {
124+
// fread will never return more than 8192 bytes if the stream is read buffered and it does not represent a plain file
125+
$chunk = fread($handle, 8192);
126+
$byteCount += strlen($chunk);
127+
$giantChunk .= $chunk;
128+
if ($byteCount >= $chunkSize)
129+
{
130+
return $giantChunk;
131+
}
132+
}
133+
return $giantChunk;
134+
}
116135
?>
117136
<div class="box">
118137
<div class="request">
119-
<?php
138+
<?php
120139
if (isset($authUrl)) {
121140
echo "<a class='login' href='" . $authUrl . "'>Connect Me!</a>";
122141
}
123142
?>
124143
</div>
125144

126145
<div class="shortened">
127-
<?php
146+
<?php
128147
if (isset($result) && $result) {
129148
var_dump($result);
130149
}

0 commit comments

Comments
 (0)