|
|
|
|
|
|
| |
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");
$ret=false;
$ftpRet=array();
if(strcmp(substr($scheme,0,3),"ftp") == 0)
{
$fp=@ftp_connect($host);
if($fp)
{
$ftpLogin=ftp_login($fp,$ftpUser,$ftpPass);
if($ftpLogin)
{
$ftpFile=$urlAry['path'];
$ftpFileAry=split('/',$ftpFile);
$numDirs=count($ftpFileAry)-1;
for($i=0; $i < $numDirs; $i++)
{
$ftpRet=ftp_raw($fp,"cwd ".$ftpFileAry[$i]);
}
$ftpRet=ftp_nlist($fp,'.');
$ftpRet=preg_grep('/^'.$ftpFileAry[$numDirs].'$/',$ftpRet);
if($ftpRet == array()) $ret="Ftp File not Found";
}
}
}
else
{
$sp=@fsockopen($host,80,$errno,$errstr);
if(!$sp) $ret=array($errno,$errstr);
else
{
fclose($sp);
$linkFile=@fopen($file,"r");
if(!$linkFile) $ret="File not Found";
else fclose($linkFile);
}
}
return $ret;
}
?> | | |
|
| Check if a file exists on a remote FTP server with PHP Categories : PHP, FTP, Regexps | | | BBCode Formatting String Categories : PHP, HTML, Regexps, Arrays | | | Tag content retrieval from websites with preg_match Categories : PHP, Regexps, Arrays, HTML and PHP | | | PHP Script to find url links in a page Categories : PHP, URLs, Regexps, Arrays | | | An array of functions to use in checking user input to HTML forms : text, firstName, middleNameOrInit, lastName, email, web, digits, decimal, hex, genNum, USD, BPS, Euro, USphone, USzip Categories : PHP, Data Validation, Regexps | | | columned txt file to array()? Categories : Arrays, Strings, Regexps, PHP | | | Validating a URL with preg_match Categories : PHP, Regexps, Beginner Guides, Data Validation | | | very simple ftp class Categories : PHP, PHP Classes, FTP | | | Array values from javascript to php Categories : PHP, Java Script, Arrays | | | clearing variables in php3 Categories : Variables, Arrays, PHP | | | Avoiding or Detecting high bit characters in a string. Useful when you want to create a valid RSS feed Categories : PHP, Strings, Unicode, Regexps, Rich Site Summary (RSS) | | | PHP Random rss feeds - selects 49 random feeds from an unlimited list and displays them on your website. It's Ideal for those moments when you got 5 minutes and dont know which one of your feeds to read. Categories : PHP, Rich Site Summary (RSS), Arrays | | | This script is a contact form between users of a
website (kinda like the PM function on the forums)
Categories : PHP, Databases, MySQL, Regexps | | | Simple way to replace a variable value in a .conf (.ini) file using a
webbrowser - the first stage of a complete universal configuration editor Categories : PHP, Regexps, Code Editors, Filesystem | | | Display list of files within current and subdirectories (recursively) showing
each file as an anchored link and each directory as a category header. Categories : Filesystem, Directories, Arrays, PHP | |
|
|
|