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
Solved

File list and XFER via FTP

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses four methods for overlaying images in a container on a web page
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…
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.

809 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