0

I want to get files from a remote server using ftp function in php. I use the ftp_nlist method to get the files but the problem is when I loop through it it contains full path with the file name. I want only file names

<?php    
$ftp_server = "host.com"; 

    $ftp_connection = ftp_connect($ftp_server) 
    or die("Could not connect to $ftp_server"); 
    $ftp_username="user";
    $ftp_userpass ="password";
    $login = ftp_login($ftp_connection, $ftp_username, $ftp_userpass);
    $path = "mainpath/assets/Template/Top/";
    $contents = ftp_nlist($ftp_connection, $path);

foreach($contents as $key=>$dat) {
echo $contents[$key];
}
?>

Which I get the files but the path contains namemainpath/assets/Template/Top/file.ext

I just want file.ext

theduck
  • 2,589
  • 13
  • 17
  • 23

1 Answers1

0

You should use basename

foreach( $contents as $key=>$dat ) {
   // To get the filename + extension
   echo basename( $contents[$key] );

   // To get the filename without extension
   $extension = explode( ".", $contents[$key] );
   echo basename( $contents[$key], '.'.$extension[1] );
}
Miquel Canal
  • 992
  • 1
  • 17
  • 25