11

I want to print the list of files and only files from an FTP server, here is what I could accomplish.

<?php
    $ftp_server = "my ftp server";
    $conn_id = ftp_connect($ftp_server);
    $ftp_user_name = "ftp username";
    $ftp_user_pass = "ftp password";
    $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
    $contents = ftp_nlist($conn_id, '/');
    for ($i = 0 ; $i < count($contents) ; $i++)
        echo "<li>" . substr($contents[$i],1) . "</li>";
    ftp_close($conn_id);
?>

but this prints the names of files and folders. How can I just print the names of files (files may not have extensions!)

sikas
  • 5,435
  • 28
  • 75
  • 120

5 Answers5

11

Options:

1) you can use ftp_rawlist instead of ftp_nlist to get the full listing for the file/directory, which should indicate whether it's a directory. However, the format of that listing will depend on the operating system of the ftp server. For example, on a unix/linux system the raw listing might look something like this:

drwxrwxr-x  3 jm72 jm72  4096 Nov  2 16:39 myDir
-rw-rw-r--  1 jm72 jm72   257 Nov  2 16:39 myFile

where the "d" in the first column will tell you it's a directory. Not sure what it would look like on a Windows server.

2) for each file name you return, try to CD into it. If you can, it's a directory!

if (ftp_chdir($conn_id, substr($contents[$i],1)) {
  //it's a directory, don't include it in your list
  ftp_cdup($conn_id) //don't forget to go back up to the directory you started in!
}
Jacob Mattison
  • 50,258
  • 9
  • 107
  • 126
  • @JacobM: it worked but gave the following error: (Warning: ftp_chdir() [function.ftp-chdir]: /index.php: No such file or directory in C:\wamp\www\Contact_Keeper\copyftp.php) on line 14 for each file that it found ... and for the structure it should be ftp_chdir($ftp_stream,$directory) (got it from the dreamweaver) – sikas Nov 03 '10 at 15:57
  • @RobertPitt: Yes, that's more readable and fits the OP's need better. I just wanted to make my example short! – Jacob Mattison Nov 03 '10 at 15:58
  • @RobertPitt: I just need the files in a specific folder. – sikas Nov 03 '10 at 15:58
  • @JacobM: I solved the error by adding error_reporting(0); at the beginning of the file. – sikas Nov 03 '10 at 15:59
  • 1
    @sikas: if the warning is a problem, you can change `ftp_chdir` to `@ftp_chdir` to suppress the warning, but be aware that will also suppress legitimate errors (for example, the network has gone down). – Jacob Mattison Nov 03 '10 at 16:04
  • Sorry, but I think I have a better solution as my way does not require changing directory – RobertPitt Nov 03 '10 at 16:08
6

Here is a script that will do it for you, courtesy of a poster on ftp_nlist (PHP Docs):

<?php

//identify directories

function ftp_is_dir($dir) {
  global $ftp_connect;
  if (@ftp_chdir($ftp_connect, $dir)) {
       ftp_chdir($ftp_connect, '..');
       return true;
  } else {
       return false;
  }
}
$ftp_nlist = ftp_nlist($ftp_connect, ".");

//alphabetical sorting

sort($ftp_nlist);
foreach ($ftp_nlist as $v) {

//1. ftp_is_dir() is true => directory
  if (ftp_is_dir($v)) {

//output as [ directory ]
      echo "[ " . $v . " ]<br />\n";
  }
}
foreach ($ftp_nlist as $v) {

//2. ftp_is_dir() is false => file
  if (!ftp_is_dir($v)) {

//output as file
      echo "" . $v . "<br />\n";
  }
}
?>
Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
  • This worked for me but I would recommend silencing the ftp_chdir with @ftp_chdir in the function's if because it will spit out warnings when trying to enter files. – Gazillion Sep 21 '11 at 19:30
  • Your code not working for me. the function always returns false. It thinks all are files not directory!!! – Mahdi Jazini Jan 03 '17 at 08:06
2

You could also use the ftp_mdtm() function. This always returns an error (-1) when it fails which would indicate a directory since this function doesn't work for directories. The one caveat is that it doesn't work on some systems (the manual doesn't state which ones), so you'd need to test if it worked first.

Lee Blake
  • 341
  • 1
  • 2
  • 15
1

If you are in the same server, you can do:

$contents = ftp_nlist($conn_id, '/');
// set the path to the folder
$path = '/home/user/public_html/my_folder/';
foreach ( $contents as $item ) {
    if ( is_file( $path . $item ) ) {
        echo $item . PHP_EOL;
    } else {
        continue;
    }
}

For remote server you can check if file exists with other methods.

Community
  • 1
  • 1
aesede
  • 5,541
  • 2
  • 35
  • 33
1

if you use the ftp_rawlist like so:

$rawfiles = ftp_rawlist($conn, true); //true being for recursive

foreach ($rawfiles as $rawfile)
{
    $info = preg_split("/[\s]+/", $rawfile, 9);
    if($info[0]{0} == 'd')
    {
        //Directory
    }else
    {
        //File
        $size = byteconvert($info[4]);
        $chmod = chmodnum($info[0]);
        $date = strtotime($info[6] . ' ' . $info[5] . ' ' . $info[7]);
    }
}

Should get you closer to your goal.

RobertPitt
  • 56,863
  • 21
  • 114
  • 161
  • 2
    This won't work if the FTP server is a Windows machine (and hasn't been configured to use Unix formatting, which can be done). In that event, you'd have to look for the `` in the listing. – Jacob Mattison Nov 03 '10 at 16:13