?
Solved

File list and XFER via FTP

Posted on 2004-10-08
2
Medium Priority
?
285 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 4

Accepted Solution

by:
duerra earned 2000 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

WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
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…
Suggested Courses

777 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