Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2009
    Location
    Rocky Face, GA
    Posts
    196

    PHP FTP Problem - Need to Copy Newest File

    I'm having a bit of an issue with the code below. The below code will copy over all files from the specified directory and it works great, but I need to be able to limit the copy to only the newest file available in the specified directory.

    PHP Code:
        while (($file readdir($handle))!==false)
        {
            if(!
    is_dir($file)){
                
    $f[]="$file";       
              }
        }
        
    closedir($handle);
        
    sort($f);
        
    $count=0;
        
    $mode FTP_BINARY// or FTP_ASCII
        
    $conn_id ftp_connect($FTP_HOST);
        if(
    ftp_login($conn_id$FTP_USER$FTP_PW)){
            
    ftp_pwd($conn_id);
            
    ftp_chdir($conn_id,$FTP_DIR);
            foreach(
    $f as $files) {
                
    $from fopen($LOCAL_SERVER_DIR.$files,"r");    
                if(
    ftp_fput($conn_id$files$from$mode)){
                    
    $count +=1;
                }
            }
            
    ftp_quit($conn_id);
        } 

    Any help is appreciated.
    Michael T. @ Resellr.Net - CEO
    The Premier Reseller Hosting Solution
    Visit Us @ Resellr.Net | Follow Resellr.Net on Twitter

  2. #2
    Join Date
    Oct 2005
    Location
    UK
    Posts
    552
    When you retrieve the list of files, sort the array by the time (last updated, created or whatever you need) and then take the first item from the array (the newest) and apply the copy function to that.

  3. #3
    Join Date
    Nov 2001
    Location
    Vancouver
    Posts
    2,416
    Or in PHP, scandir is shorter than readdir variant but whatever - here are two options, perhaps a more current PHP whiz might do it differently. 8 - 10 lines of code plus another 8 or so for the ftp routine:

    PHP Code:
    function get_latest($dir) {
        
    $dirfiles scandir($dir);
        
    $files = array();
        foreach(
    $dirfiles as $fname) {
            
    $path $dir '/' $fname;
            if(
    is_file($path)) {
                
    $files[filemtime($path)] = $fname;
                }
            }
        
    ksort($files);
        return 
    array_pop($files);
        }

    ftp_this(get_latest('.')) 

    ... or ...

    PHP Code:
    function get_latest2($dir) {
        if (
    $handle opendir($dir)) {
            
    $files = array();
            while (
    false !== ($fname readdir($handle))) {
                
    $path $dir '/' $fname;
                if(
    is_file($path)) {
                    
    $files[filemtime($path)] = $fname;
                    echo 
    "$path<br>";
                    }
                }
            
    closedir($handle);
            }
            
    ksort($files);
            return 
    array_pop($files);
        }

    ftp_this(get_latest('.')) 
    “Even those who arrange and design shrubberies are under
    considerable economic stress at this period in history.”

  4. #4
    Join Date
    Mar 2009
    Location
    Rocky Face, GA
    Posts
    196
    Thanks much, I'll give that a go and see if that works here shortly . I do appreciate the help!
    Michael T. @ Resellr.Net - CEO
    The Premier Reseller Hosting Solution
    Visit Us @ Resellr.Net | Follow Resellr.Net on Twitter

  5. #5
    Join Date
    Nov 2001
    Location
    Vancouver
    Posts
    2,416
    My pleasure. Here's the same thing implemented in Python:

    PHP Code:
    from os import pathlistdir
    import ftplib

    # Sometimes this is the most efficient form:

    def latest_file_old_school(dir):
        
    """Employs old-school yet still useful for loop"""
        
    files = []
        for 
    f in listdir(dir):
            
    fpath path.join(dirf)
            if 
    path.isfile(fpath):
                
    files.append((path.getmtime(fpath), fpath))
        return 
    sorted(files)[-1][1] if files else None

    # Other times a "list comprehension" can tighten up code in a 
    # visually beneficial, not so artificial way; here's an alternative
    # that also benefits from splitting the code up into list_files and 
    # latest_file functions, a logical move on the road for re-usability

    def list_files(dir):
        
    """(dir:string) -> [fpath:string,] | None

        Returns a list of full paths rather than just filenames; employs a 'list
        comprehension' to collapse 4 lines of code into 1 or 2, albeit
        in this case with the cost of some additional string manipulation.
        """
        
    dir path.join(dir''# ensure terminated with proper path separator
        
    return [dir+fname for fname in listdir(dir) if path.isfile(dir+fname)]

    def latest_file(dir):
        
    """(dir:string) -> string | None
        """
        
    files = [(path.getmtime(fpath), fpath) for fpath in list_files(dir)]
        return 
    sorted(files)[-1][1] if files else None

    def cp_ftp
    (hostuserpasswdfnameas_file=None):
        
    # rudimentary
        
    as_file as_file if as_file else path.split(fname)[-1]
        
    ftp ftplib.FTP(host)
        
    ftp.login(userpasswd)
        
    ftp.storbinary('STOR %s' as_fileopen(fname'rb'))
        
    ftp.quit()

    if 
    __name__ == '__main__':
        
    cp_ftp('someftphost.com''jenny''8675309',
               
    latest_file('/path/to/my/updating/files'),
               
    as_file='public_html/latest_foo.txt'
    The username and passwd is a nod to the song sung by Tommy Tutone in 1982, thus dating myself. Was in the news today - where a phone number 867-5309 was up for grabs for a price.

    http://en.wikipedia.org/wiki/867-5309/Jenny
    Last edited by mwatkins; 08-01-2009 at 04:22 AM.
    “Even those who arrange and design shrubberies are under
    considerable economic stress at this period in history.”

Similar Threads

  1. Slow file copy on XP Pro
    By MGCJerry in forum Computers and Peripherals
    Replies: 17
    Last Post: 04-24-2009, 03:46 PM
  2. Copy a file into EVERY folder?
    By Zoltrix in forum Web Hosting Lounge
    Replies: 15
    Last Post: 03-01-2006, 04:10 AM
  3. how to copy file with out shell ??
    By hossamsoft in forum Programming Discussion
    Replies: 4
    Last Post: 11-28-2004, 11:59 AM
  4. Using Multiple file Copy in WHM
    By TimBraz in forum Hosting Security and Technology
    Replies: 1
    Last Post: 09-23-2004, 03:05 PM
  5. Copy file
    By BigJ406 in forum Hosting Security and Technology
    Replies: 2
    Last Post: 02-16-2003, 10:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •