|
-
Feb 23rd, 2017, 04:29 PM
#1
List ftp files/folders
I'm trying to connect to an ftp site and list the files and folders (and subfolders) but all I'm getting is an empty array, it connects fine, but I'm getting nothing from the ftp_nlist() function. When I log into the ftp site with FileZilla there's a whole directory structure available, any ideas to why this doesn't work with php?
Code:
<html>
<head>
<title>Ftp List</title>
</head>
<body>
<?php
function ftpRecursiveFileListing($ftpConnection, $path) {
static $allFiles = array();
$contents = ftp_nlist($ftpConnection, $path);
if (is_array($contents)) {
foreach($contents as $currentFile) {
// assuming its a folder if there's no dot in the name
if (strpos($currentFile, '.') === false) {
ftpRecursiveFileListing($ftpConnection, $currentFile);
}
$allFiles[$path][] = substr($currentFile, strlen($path) + 1);
}
}
return $allFiles;
}
// connect and login to FTP server
$ftp_server = "<removed for forum post>";
$ftp_userName = "<removed for forum post>";
$ftp_password = "<removed for forum post>";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_userName, $ftp_password);
if ($login) {
// get file list of current directory
$file_list = ftpRecursiveFileListing($ftp_conn, ".");
if (!empty($file_list)) {
foreach($file_list as $file) {
echo("$file<br />");
}
} else {
echo("No directory list");
}
// close connection
ftp_close($ftp_conn);
} else
echo("Login failed on <b>$ftp_server</b> for <b>$ftp_userName</b>");
?>
</body>
</html>
I'm using xampp for coding/testing this, but when I upload it to the GoDaddy PHP webserver it's not working their either.
-
Feb 24th, 2017, 09:10 AM
#2
Re: List ftp files/folders
After more trial and error with my code I've determined it has to be some PHP environment setting, but saddly I'm not that familiar with PHP to even know where/what settings there are, anyone point me in the right direction?
-
Feb 24th, 2017, 09:19 AM
#3
Re: List ftp files/folders
Except for one point, it seems like it should be right... but I can't quite place my finger on it...
the one place that doesn't look right to me is this:
Code:
if (strpos($currentFile, '.') === false) {
ftpRecursiveFileListing($ftpConnection, $currentFile);
}
You're not assigning the result from the ftpRecursiveFileListing to anything... but $allFiles is static, and it would be added to it in there, so maybe it's ok.
Have your tries var_dumping $contents to see if the call of nlist is returning anything? That might be worth looking into.
Also, you may want to look at ftp_rawlist ... it has a recursive parameter... it returns a lot more than nlist does, but it also handles the recursive folder spelunking for you.
-tg
-
Feb 24th, 2017, 10:20 AM
#4
Re: List ftp files/folders
 Originally Posted by techgnome
Except for one point, it seems like it should be right... but I can't quite place my finger on it...
the one place that doesn't look right to me is this:
Code:
if (strpos($currentFile, '.') === false) {
ftpRecursiveFileListing($ftpConnection, $currentFile);
}
You're not assigning the result from the ftpRecursiveFileListing to anything... but $allFiles is static, and it would be added to it in there, so maybe it's ok.
Have your tries var_dumping $contents to see if the call of nlist is returning anything? That might be worth looking into.
Also, you may want to look at ftp_rawlist ... it has a recursive parameter... it returns a lot more than nlist does, but it also handles the recursive folder spelunking for you.
-tg
I tried changing it to ftp_rawlist() but it still doesn't give me anything, here's my current code:
Code:
<html>
<head>
<title>Ftp List</title>
</head>
<body>
<?php
// connect and login to FTP server
$ftp_server = "<removed for forum post>";
$ftp_userName = "<removed for forum post>";
$ftp_password = "<removed for forum post>";
$dir = '/';
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_userName, $ftp_password);
if ($login) {
// get file list of current directory
$file_list = ftp_rawlist($ftp_conn, $dir, true);
if (!empty($file_list)) {
echo "<ul>";
foreach($file_list as $file) {
echo("<li>$file</li>");
}
echo "</ul>";
} else {
echo("No directory list");
}
// close connection
ftp_close($ftp_conn);
} else
echo("Login failed on <b>$ftp_server</b> for <b>$ftp_userName</b>");
?>
</body>
</html>
I'm fairly certain that either PHP no longer does ftp, or I need to change a setting but I haven't a clue what it'd be that I would need to change.
I'm able to do all this stuff super easily in .Net though, not that knowing that helps anything here.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|