Solved

File list and XFER via FTP

Posted on 2004-10-08
2
271 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

762 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