Skip to content

Instantly share code, notes, and snippets.

@ericboehs
Created August 20, 2009 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericboehs/171163 to your computer and use it in GitHub Desktop.
Save ericboehs/171163 to your computer and use it in GitHub Desktop.
<?
echo "<pre>";
// This example should work but some http servers will not respond properly to a HEAD request
// i.e. google.com
$url = 'http://ericboehs.com/uploads/gahr-1.png';
$ch = curl_init();
if (!$ch)
return false;
curl_setopt($ch, CURLOPT_URL, $url); //Set the url
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //Gets the server response
curl_setopt($ch, CURLOPT_HEADER, true); // Gets headers from server
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'HEAD');
$response = curl_exec($ch); //Execute the query and save the response
curl_close($ch); //Close the connection
$response = explode("\n",$response);
foreach($response as $line){
if(strpos($line, "ontent-Length")) //Yes I know I forgot the C
$content_length = trim(str_replace("Content-Length: ",'',$line));
}
echo "The file size is ".$content_length." bytes. <br />\n";
die('End of Example 1');
// This example doesn't work as the intention is to just get the header and fopen reads the whole
// file in at once.
$file = 'http://www.example.com/file.jpg';
$fp = fopen( $file, 'r' );
$data = stream_get_meta_data($fp);
fclose($fp);
print_r($data);
die('End of Example 2');
// This example doesn't work as file_get_contents only supports seeking on certain file types.
// I've used file_get_contents to seek on a non-local file before. Matter of fact it's running
// right now as a cronjob.
$url = 'http://ericboehs.com/uploads/gahr-1.png';
$file_position = 0;
$file_chunk = file_get_contents($url, NULL, NULL, $file_position, 100);
$i=0;
while(strpos($file_chunk, 'Content-Length') === FALSE || $i=210){
$file_position += 100;
$file_chunk = file_get_contents($url, NULL, NULL, $file_position, 100);
echo $file_chunk."<br/>\n";
$i++;
}
die('End of Example 3');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment