Solved

File list and XFER via FTP

Posted on 2004-10-08
2
259 Views
Last Modified: 2008-03-10
I know this will be difficult...I want to create a link to a .php file which runs code to generate a list of files in a folder (along with subfolders and files) and send those files to a ftp server. Can it be done?
0
Comment
Question by:CDCOP
2 Comments
 
LVL 33

Expert Comment

by:snoyes_jw
ID: 12262449
0
 
LVL 4

Accepted Solution

by:
duerra earned 500 total points
ID: 12264917
Oh come on.  Help the poor soul.  He's offering 500 points

Try something like this, CDCOP.  I haven't tested it, but I don't think you'll run into too many problems:

function getTheFiles($directory, $recursive, $masks=false)
{
      $matches = array();
      if(is_dir($directory))
      {
            if(@chdir($directory))
            {
                  $handle=opendir('.');
                  if($handle)
                  {
                        while( ($file = readdir($handle)) !== FALSE)
                        {
                              if(is_dir($file))
                              {
                                    if($recursive)
                                    {
                                          if(substr($directory, -1, 1) != '/' AND substr($directory, -1, 1) != '\\')
                                          {
                                                $directory.= '/';      
                                          }
                                          $returned = getTheFiles($directory.$file, $recursive, $masks);
                                          foreach($returned AS $afile)
                                          {
                                                $matches[] = $afile;      
                                          }
                                    }
                              }
                              
                              if($masks)
                              {
                                    if(!is_array($masks))
                                    {
                                          $masks = array($masks);      
                                    }
                                    foreach($masks AS $mask)
                                    {
                                          if(strstr($file, $mask))
                                          {
                                                $matches[] = $file;
                                                break;      
                                          }
                                    }
                              }
                              else
                              {
                                    $matches[] = $file;
                              }
                        }
                        close($handle);
                  }
                  else die("Couldn't open directory $directory");
            }
            else die( "Couldn't access directory $directory");
      }
      else die('not a directory');      
      
      return $matches;
}

$files = getTheFiles('/path/to/a/dir', true);

$conn = ftp_connect('host', 'port');
if(!$conn) die('Could not connect to ftp server');

$login = ftp_login($conn, 'username', 'password');
if($login)die('Could not login to ftp server');

foreach($files AS $file)
{
      if(!ftp_put($conn, '/path/to/remote/file', $file))
      {
            echo 'Could not upload file '.$file.'<Br>';      
      }      
}

ftp_close($conn);
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now