WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Mobile Dev World

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Check for functional file links (broken Files)
Categories : PHP, Data Validation, FTP, Regexps, Arrays Click here to Update Your Picture
Dave Silvia
Date : Nov 10th 2006
Grade : 2 of 5 (graded 2 times)
Viewed : 5389
File : 4527.php
Images : No Images for this code example.
Search : More code by Dave Silvia
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

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
Banknote Validation - A PHP class that provides several methods to quickly validate banknote serial numbers of the following currencies: AUD, CAD, CHF, CNY, EUR, GBP, JPY, USD.
Categories : PHP, PHP Classes, Data Validation, Regexps
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
grab the result of any calculation you submit to the Google Calculator.
Categories : PHP, Arrays, Web Services, Regexps, Math.
The GTV.class allows you to extract a value between any HTML tag or between any TEXT on a web page.
Categories : PHP, PHP Classes, Regexps
Remote Archive (Zip, Tar, Gzip) downloader with FTP and local extration support
Categories : PHP, FTP, Filesystem, PHP Classes, Compression
validateEmail 2.0 - upgraded version of the old validateEmail function used to validate email addresses via SMTP and regex.
Categories : Email, Regexps, PHP
Link Submition - Allow your visitors to submit links to the site.
Categories : PHP, Arrays, Filesystem, Beginner Guides
Array Insertion
Categories : PHP, PHP Classes, Arrays
Making sure a string containes only digits or no digits.
Categories : Strings, PHP, Regexps