Accessing server through FTP

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Accessing server through FTP

Post 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?
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Accessing server through FTP

Post 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
Attachments
ftp.zip
(3.62 KiB) Downloaded 98 times
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: Accessing server through FTP

Post 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.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Accessing server through FTP

Post 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.
Post Reply