Validate that a link to a file is reachable
(c) 2006, D.E. Silvia, All rights reserved.
This code is available for use for non-commercial purposes.
Free to distribute as long as this copyright information remains intact.
No modification is authorized. Please, refer bugs/enhancements to
dsilvia@mchsi.com
Function to check for functional file links
Allows you to check legitimacy of user input for links. You could also place in
crontab (Uinices) or Scheduled Tasks (Windows) to periodically verify the links
you have in place.
A simple example of usage is included in the comment below, simply uncomment it
as indicated for a demonstration of how the code works.
Function has a single argument, $file, which is the url to the linked file.
Return is boolean 'true', 'false', error array, or short string describing the error.
Errors:
false: ftp_connect() failed
array: error# and error string from fsockopen()
string: "No path in ftp url"
"[Ftp ]File Not Found"
brokenFileLink.php
<?php
$fileAry[0]='http://weberforums.com/index.php';
$fileAry[1]='http://weberdev.com/index.html';
$fileAry[2]='http://www.google.com/advanced_search?q=windows+xp&hl=en&lr=lang_en&as_qdr=m3';
$fileAry[3]='ftp://anonymous:anonymous@mirror.services.wisc.edu/pub/mirrors/mysql/Downloads/MySQL-5.0/mysql-5.0.26-win32.zip';
$fileAry[4]='ftp://mirrors.24-7-solutions.net/pub/mysql/Downloads/MySQL-5.0/mysql-5.0.26-win32.zip';
$fileAry[5]='ftp://anonymous:anonymous@ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-6.7.tar.gz';
$fileAry[6]='ftp://mirror.services.wisc.edu';
for($i=0; $i < count($fileAry); $i++)
{
$file=$fileAry[$i];
print("<h4>File: $file</h4>");
flush();
$linkBroken=brokenLink($file);
if($linkBroken)
{
print("Link to $file is broken<br />");
if(is_array($linkBroken)) print("Problem with host: errno#".$linkBroken[0].", error:".$linkBroken[1]."<br />");
else print("Problem with file: $linkBroken<br />");
flush();
}
else
{
print("Link to $file is okay<br />");
flush();
}
}
/*
*
*/
function brokenLink($file)
{
$urlAry=parse_url($file);
$host=$urlAry['host'];
$scheme=$urlAry['scheme'];
// set these however you like. The old 'anonymous/anonymous'
// will not work on many sites as they expect an email format
// for the password.
if(isset($urlAry['user'])) $ftpUser=$urlAry['user']; else $ftpUser='anonymous';
if(isset($urlAry['pass'])) $ftpPass=$urlAry['pass']; else $ftpPass='me@home';
if(!isset($urlAry['path'])) return "No path in url";
$isFTP=!strcmp(substr($scheme,0,3),"ftp");