2

How do I get the filesize of js file on another website. I am trying to create a monitor to check that a js file exists and that it is more the 0 bytes.

For example on bar.com I would have the following code:

$filename = 'http://www.foo.com/foo.js';
echo $filename . ': ' . filesize($filename) . ' bytes';
Ben Paton
  • 1,432
  • 9
  • 35
  • 59

4 Answers4

3

You can use a HTTP HEAD request.

<?php
 $url = "http://www.neti.ee/img/neti-logo.gif";
 $head = get_headers($url, 1);
 echo $head['Content-Length'];
?>

Notice: this is not a real HEAD request, but a GET request that PHP parses for its Content-Length. Unfortunately the PHP function name is quite misleading. This might be sufficient for small js files, but use a real HTTP Head request with Curl for bigger file sizes because then the server won't have to upload the whole file and only send the headers.

For that case, use the code provided by Jakub.

Uku Loskit
  • 40,868
  • 9
  • 92
  • 93
  • 1
    note that `get_headers` by default does a GET request. See Example 2 in the manual on how to change that. – Gordon Jan 11 '12 at 14:26
1

Just use CURL, here is a perfectly good example listed:
Ref: http://www.php.net/manual/en/function.filesize.php#92462

<?php
$remoteFile = 'http://us.php.net/get/php-5.2.10.tar.bz2/from/this/mirror';
$ch = curl_init($remoteFile);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //not necessary unless the file redirects (like the PHP example we're using here)
$data = curl_exec($ch);
curl_close($ch);
if ($data === false) {
  echo 'cURL failed';
  exit;
}

$contentLength = 'unknown';
$status = 'unknown';
if (preg_match('/^HTTP\/1\.[01] (\d\d\d)/', $data, $matches)) {
  $status = (int)$matches[1];
}
if (preg_match('/Content-Length: (\d+)/', $data, $matches)) {
  $contentLength = (int)$matches[1];
}

echo 'HTTP Status: ' . $status . "\n";
echo 'Content-Length: ' . $contentLength;
?>

Result:

HTTP Status: 302
Content-Length: 8808759

Jakub
  • 20,418
  • 8
  • 65
  • 92
0

Another solution. http://www.php.net/manual/en/function.filesize.php#90913

message
  • 4,513
  • 2
  • 28
  • 39
  • While this may theoretically answer the question, [it would be preferable](http://meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Bill the Lizard Jan 11 '12 at 14:23
0

This is just a two step process:

  1. Crawl the the js file and store it to a variable
  2. Check if the length of the js file is greater than 0

thats it!!

Here is how you can do it in PHP

<?php

$data = file_get_contents('http://www.foo.com/foo.js');

if(strlen($data)>0):
    echo "yay"
else:
    echo "nay"
?>

Note: You can use HTTP Head as suggested by Uku but then if you are seeking for the page content if js file has content then you would have to crawl again :(

codersofthedark
  • 9,183
  • 8
  • 45
  • 70