|Back to examples
Just some rough scripts showing uses for PHPs FTP library.
One of the biggest ways I have used FTP is a cheat for those servers which
will not give Apache permission to create local directories. Assuming that the
FTP module is install, just FTP to yourself and create the directories the hard
way. Not ideal by any stretch of the imagination, but it is a work around.
The below script is a cheat that I used at an old employers company. The issue
was we needed dynamic creation of directories on a webserver which we had no permissions
to other than via FTP ( and the PHP FTP module was not installed either ). So,
I rigged a scheduled script which ran locally to check which directories needed
to be created, which in turn called a script on a remote server which would FTP
the directories back.
This is by no means the best way of doing things, but, sometimes you have to
be a bit messy. And, incase you are wondering, the bigger version of these scripts
are still in use today!!
We check for the existence of the directory prior to calling the creation script
as FTP does use a lot of resource, the less work we do the better.
This is the local file ( if needed )
// This is the directory we want to create
$dir = "newdir";
// This is the URL on the remote server of the
// script that is going to FTP back to us.
$url = "http://ftpenabled.server.com/ftp.php?dir=$dir";
// This just checks if the directory already exists ....
// if it doesn't call our remote script to FTP back to
// us and create it.
// Also check "allow_url_fopen" is enabled.
// Now open the URL. The remote script can pass data back to
// us in $var
$fh = fopen($url,"r");
// Just to stop it timing out ... FTP isn't fast!
$var = fgets($fh, 1024);
This is the remote file http://ftpenabled.server.com/ftp.php
// If the server where this file is located
// has the FTP portion of PHP enabled then
// you call just run this function locally.
// Dir is the directory to create
$dir = $HTTP_GET_VARS["dir"];
// These 3 variable you could pass to the
// script within the URL if you wanted -
// bit insecure though
$ftp_ip = "ftp.this.com";
$ftp_username = "username";
$ftp_password = "password";
// Set to PASV mode
ftp_pasv( $ftp, 1);
// In this example set the current directory to
// If we cannot set our directory to the new one
// then we create it
echo("Directory $dir created ok");