David
David

Reputation: 2232

find public html folder using php's ftp functions

I have a php script that logs into my servers via the ftp function, and backs up the entire thing easily and quickly, but I can't seem to find a way to let the script determine the folder where the main index file is located.

For example, I have 6 servers with a slew of ftp accounts all set up differently. Some log into the FTP root that has httpdocs/httpsdocs/public_html/error_docs/sub_domains and folders like that, and some log in directly to the httpdocs where the index file is. I only want to backup the main working web files and not all the other stuff that may be in there

I've set up a way to define the working directory, but that means I have to have different scripts for each server or backup I want to do.

Is it possible to have the php script find or work out the main web directory? One option would be to set up a database that has either the directory to use or nothing if the ftp logs in directly to that directory, but I'm going for automation here.

If it's not possible I'll go with the database option though.

Upvotes: 0

Views: 371

Answers (2)

DaveRandom
DaveRandom

Reputation: 88647

You cannot figure out through FTP alone what the root directory configured in apache is - unless you fetch httpd.conf and parse it, which I'm fairly sure you don't want to do. Presumably you are looping to do this backup from multiple servers with the same script?

If so, just define everything in an array, and loop it with a foreach and all the relevant data will be available in each iteration.

So I would do something like this:

// This will hold all our configuration
$serverData = array();

// First server
$serverData['server1']['ftp_address'] = 'ftp://11.22.33.44/';
$serverData['server1']['ftp_username'] = 'admin';
$serverData['server1']['ftp_password'] = 'password';
$serverData['server1']['root_dir'] = 'myuser/public_html';

// Second server
$serverData['server2']['ftp_address'] = 'ftp://11.22.22.11/';
$serverData['server2']['ftp_username'] = 'root';
$serverData['server2']['ftp_password'] = 'hackmeplease';
$serverData['server2']['root_dir'] = 'myuser/public_html';

// ...and so on
// Of course, you could also query a database to populate the $serverData array

foreach ($serverData as $server) {

  // Process each server - all the data is available in $server['ftp_address'], $server['root_dir'] etc etc

}

Upvotes: 2

Marc B
Marc B

Reputation: 360662

No, you can't do it reliably without knowledge of how Apache is setup for each of those domains. You'd be better off with the database/config file route. One-time setup cost for that plus a teensy bit of maintenance as sites are added/modded/removed.

You'll probably spend days getting a detector script going, and it'll fail the next time some unknown configuration comes up. Attemping to create an AI is hard... you have to get it to the Artificial Stupidity level first (e.g. the MS Paperclip).

Upvotes: 0

Related Questions