Page 1 of 1

Accessing server through FTP

Posted: Tue Apr 14, 2009 5:09 pm
by allspiritseve
I need to access a remote server through FTP and display all of the files and directories for download (read-only). I know PHP has built-in ftp functions, but I've also seen examples where a socket was opened (I've never done anything like this before, is one better than the other?). Will there be any problems if I'm using a single ftp login? Is there a max to how many users can be logged on with a single account at once? I've played around with the PHP functions a little, and I can't find a way to list only directories. Is there a way to do that using a socket?

Re: Accessing server through FTP

Posted: Tue Apr 14, 2009 9:03 pm
by alex.barylski
You have three options:

1. Implement the FTP protocol yourself using sockets (lots of work -- I think SwiftMailer does this)
2. Use the FTP extension to avoid implementing that API yourself using sockets
3. Enable FTP as a schema and use the regular file access functions (fopen, etc)

There are some limitations to using the latter -- what they are exactly escape me at the moment.

I wrote an FTP abstraciton layer a while back and implemented drivers for #2 and #3...if I find it in the next 2 minutes it's yours to play with:

EDIT | I lied...I looked quickly and it seems the 'extension' driver I didn't implement but the interface is there for you to implement. The 'wrapper' is the driver which I believe uses native file functions but requires FTP URL wrappers to be enabled in php.ini.

The sole advantage of using sockets would be that your host wouldn't require the FTP extension or FTp URL to be enabled. That and you would have full control over the FTP implementation.

Cheers,
A;ex

Re: Accessing server through FTP

Posted: Sat Apr 18, 2009 4:40 pm
by allspiritseve
Cool, thanks for that. I think I will use #2.

Is there any way to safeguard against people accessing directories they're not supposed to be in? It's not a huge deal, since the lowest directory they could access is the ftp root, but ideally I'd like to be able to restrict a specific user to browsing only within a certain directory. All users will be browsing through the website, so the script will be using the same username/password for every user.

Re: Accessing server through FTP

Posted: Sun Apr 19, 2009 6:33 pm
by alex.barylski
Is there any way to safeguard against people accessing directories they're not supposed to be in?
Sure, you could implement the checks in your PHP code to prevent browsing of select directories, or you could 'probably' disable directories at the FTP server level -- but that would require using a FTP server which supported something like that -- I haven't used FTP in a couple years so I can't tell which which server might do this.
All users will be browsing through the website, so the script will be using the same username/password for every user.
In that case, I think I would just implement the checks in PHP code so viewable directories/files/actions are completely configurable.