The File Info extension

Fileinfo is a PHP script that you can use from your Ajax application to get essential informations about a file on the server or any website:

The XMLHttpRequest object has a similar function, getResponseHeader but it depends upon the configuration of the server and often doesn't return what you expect from it.

How it works

The PHP script currently holds a single function, fileinfo, with an argument: the name of a file or a URL.
The function makes use of some PHP functions (compatible with all version of PHP) to get the infos:

These infos are packed into an array with these keys in order: date, size, type and this array is returned by the function.
To use the function, call it with a such code:

include_once("fileinfo.php");
$x = fileinfo("someurl.html");
print_r($x);

These infos are displayed and also stored into a file in the current directory, fileinfo.txt, one info per line.
You can then make a POST request to call the PHP script, with the filename as parameter, and use responseText for the result.

Example of result:

Thu, 28 Jun 2007 19:37:18 GMT
1575
file

View the PHP extension (renamed fileinfo-php.txt).

Ajax demo with the HEAD method

Call a script, actually included in this page with a filename as parameter. The PHP script retrieves essential infos about the file and display them below in this page.

Result

HTML code:

<input type="text" id="filename" value="anaa-file-info.php">
<input type="button" value="Informations" onClick="submitForm('filename')">

<div id="zone"></div> 

Ajax and JavaScript code:

<script src="anaa.js" type="text/javascript"></script>
<script language="JavaScript">
function submitForm(id) { var url = document.getElementById(id); var xhr=AACreate(); xhr.onreadystatechange=function() { if(xhr.readyState == 4) { var h = xhr.getAllResponseHeaders(); document.getElementById("zone").innerHTML = "Headers:<br/>"; document.getElementById("zone").innerHTML += h; } }; xhr.open("HEAD", url, true); xhr.send(null); } </script>

Demo using Anaa by chaining POST to execute a PHP script and GET to retrieve the results

This demonstration use the Anaa framework.

It uses the fileinfo.php PHP script with a POST Ajax command to pass parameters to the script, in this case the URL of a page on which you want information. The results are retrieved by the JavaScript script in the same way we do it with the GET method, using the responseText attribute (or XML response).

Type the URL of a file:

Result
Empty.
Empty.

HTML code:

<form method="POST" action="">
  <input type="text" id="filename" value="" size="80">
  <input type="button" value="Retrieve"  onClick="submitFormAnaa()">
</form>

<div id="storage"></div> 

JavaScript source code:

function retrieve(content)
{
  var storing = document.getElementById("storage");
  content = content.replace(/\n/, "<br>");
  storing.innerHTML = content;
}

function submitFormAnaa()
{ 
  var content = document.getElementById("filename").value;
  AAWrite("chain-fileinfo.php", "url=" + content, retrieve);
} 

PHP code:

<?php
include_once("fileinfo.php");
$url=$_POST['url'];
fileinfo($url);
?>

fileinfo.php extension:

function fileinfo($filename)
{
  $pagedate = date ("D, d M Y H:i:s", filemtime($filename)) . " GMT";
  $pagesize = filesize($filename);
  $pagetype = filetype($filename);
  echo  "Date: ", $pagedate."<br>";
  echo  "Size: ", $pagesize."<br>";
  echo  "Type: ", $pagetype."<br>";
}	

$url=$_POST['url']; fileinfo($url);

The code in this page is the code of the archive simplified a bit but is functional.

© 2007-2019 Scriptol.com