0

The goal is to use php to FTP into a server and get the directory listings. The only problem is that I need to use the absolute path to get to the directory.

Example:

$host = "example.com";
$port = 21;
$connection = ftp_connect($host, $port);

ftp_login($connection, "userA", "password");
ftp_pasv($connection, true);

//user's home directory is /home/userA
print_r( ftp_nlist($connection, "logs")); //works as intended
print_r( ftp_nlist($connection, "/home/userA/logs")); //does not work

The really confusing part is that using the filesystem functions with an ftp url gives me the exact opposite problem:

opendir("ftp://userA:password@example.com:21/home/userA/logs"); //works
opendir("ftp://userA:password@example.com:21/logs"); //does not work

Since the user/directory is inputted by a user, I won't necessarily know if they want to use an absolute or relative (home directory) path. I could use a simple strstr to check if the first character in the path is a "/" and choose which method to go with, but then I would end up writing twice as much code.

There's got to be an easier way to get some method to work with both absolute and relative paths. I'm hoping it's something simple that I'm just overlooking.

Thanks

user1052474
  • 73
  • 1
  • 5
  • That sounds like a problem with your FTP server. I tried it on a couple servers I have access to and it worked fine either way. Can you check the user-entered path and see if it starts with the current directory (`ftp_pwd($connection)`)? If it does, remove the beginning of the path. – mcrumley Aug 29 '12 at 18:08
  • `ftp_pwd($connection)` returns "/" only. I was expecting it to return "/home/userA". I can't think of another way to find the user's default home directory via ftp. Admittedly, I'm no server admin. I also don't have access to change anything on the ftp server and, unfortunately, only have this one to test on. – user1052474 Aug 29 '12 at 18:30
  • Unfortunately, I couldn't find a good solution, so I just wrote two classes: one for absolute paths and one for relative paths. – user1052474 Sep 11 '12 at 17:14
  • I found that if you use ftp_chdir('/a/b/c') and then ftp_nlist('.'), then it will output the file *names* relative to the current directory. However, if you use ftp_nlist('/a/b/c'), you get the absolute file paths. Poorly documented on php.net. – donquixote May 27 '15 at 08:00

2 Answers2

0

Looks like the FTP user is not defined properly. No chance you can contact the ftp admins? You didn't write under what OS the FTP is running and which FTP service, here are two examples of how to set the home directories:

Linux with VSFTP

Create a user and set his home directory, than edit /etc/vsftpd/vsftpd.conf:

# Uncomment this to allow local users to log in.
local_enable=YES

[[ snip ]]

# You may specify an explicit list of local users to chroot() to their home
# directory. If chroot_local_user is YES, then this list becomes a list of
# users to NOT chroot().
#chroot_list_enable=YES
# (default follows)
#chroot_list_file=/etc/vsftpd.chroot_list

Windows

create a user account , set a folder within the FTP server and give that user the permission for full access, and configure the home page for the user:

  1. In Control Panel, open Administrative Tools.
  2. Double-click Computer Management.
  3. Expand Local Users and Groups, click Users, right-click the user name, and then click Properties.
  4. Click the Profile tab.
  5. Make sure that Local path is selected under Home Folder, and then type the appropriate path in the Local path box. For example, type the path as d:\Ftp\FolderName.
Kuf
  • 17,318
  • 6
  • 67
  • 91
0

Open file vsftpd.conf in /ect/vsftpd: Insert line:

anon_root= /var/www/html
user2214236
  • 173
  • 1
  • 3