PX : code

FTP Access by Ray Adams
Download this code


<?php
#
# author Ray Adams
# (c) 2001 by Ray Adams
# created 09.03.2001
#

#
# Simply example on How to access FTP server
#

//functions
$ftp_id=0;

function 
mysort ($a,$b,$in_id)
         {

            if (
$a == $b) return 0;

            
//if both is directory
            
if ((ftp_size($in_id,$a)==-1) &&
            (
ftp_size($in_id,$b)==-1))
                {
                return (
$a $b) ? -1;
                }

            
//if first file is directory
            
if ((ftp_size($in_id,$a)==-1) &&
            (
ftp_size($in_id,$b)!=-1))
            {
               return -
1;
            }

            
//if second file is directory
            
if ((ftp_size($in_id,$a)!=-1) &&
            (
ftp_size($in_id,$b)==-1))
            {
               return 
1;
            }

            
//if both parameters is files
            
if ((ftp_size($in_id,$a)!=-1) &&
            (
ftp_size($in_id,$b)!=-1))
            {
               return (
$a $b) ? -1;
            }

         }
//
$show=true;
echo 
"<head>
<style>
<!--
td           { font-family: Verdana; font-size: 10pt }
th           { font-family: Verdana; font-size: 10pt }
-->
</style>
</head>
<body>
<font face=Verdana size=2>\";
if (isset($action) && ($action=\"open\"))
   {
      if (isset($FTPURL) && ($FTPURL!=\"\"))
      {
         @$ftp_id=ftp_connect($FTPURL);
         if ($ftp_id)
         {
            echo \"FTP Site : <b>\".$FTPURL.\"<br></b>\";
            if (@ftp_login($ftp_id,\"anonymous\",\"test@user.com\"))
               {
               //Reading FTP Directory
               echo \"<hr>\";

               $ftp_list=ftp_nlist($ftp_id,\"/\");
               usort($ftp_list,mysort(\"\",\"\",$ftp_id));
               echo \"<table width=50%>\";
               echo \"<tr>
                     <th bgcolor=\'#0000FF\'><font color=yellow>Name</th>
                     <th bgcolor=\'#0000FF\'><font color=yellow>Size/Type</th>
                     </tr>\";
               for ($i=0; $i<count($ftp_list) ;$i++)
                   {
                      $file_size=ftp_size($ftp_id,$ftp_list[$i]);
                      echo \"<tr>\";
                      $font_color=($file_size==-1) ? \"<font color=blue>\" : \"<font color=black>\";
                      echo \"<td width=\'50%\'>\".$font_color.substr($ftp_list[$i],1,255).\"</td>\";
                      echo \"<td align=right>\";
                      if ($file_size!=-1)
                         {
                         echo number_format($file_size,0,\".\",\" \").\" b\";
                         } else
                         {
                         echo \"-subdir-\";
                         }

                      echo \"</td></tr>\";
                   }

               } else
               {
               echo \"Error logging to FTP server<br>\";
               }
               echo \"</table>\";
            ftp_quit($ftp_id);
            $show=false;
          } else
          {
          //Error opening FTP site
            echo \"Error opening FTP Site :\".$FTPURL;
          }
      } else
      {
         echo \"You must enter site!<br>\";
      }
   }
   else
   {
   }
if ($show) {

 echo \"<form method=\'POST\' action=\'ftpac.php3?action=open\'>
  <p>
  Enter FTP site to open
  <input type=\'text\' name=\'FTPURL\' size=\'20\'>
  <input type=\'submit\' value=\'Open\' name=\'B1\'>
  <input type=\'reset\' value=\'Clear\' name=\'B2\'>
  </p>
  </form>\";
  }
?>

Comments or questions?
PX is running PHP 5.2.17
Thanks to Miranda Productions for hosting and bandwidth.
Use of any code from PX is at your own risk.