Solved

File list and XFER via FTP

Posted on 2004-10-08
2
252 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

746 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

18 Experts available now in Live!

Get 1:1 Help Now