Conversion tables A-K
  Conversion tables L-Z
  SPAM database query
  Reverse WHOIS utility
  North America area codes
  PHP Example forums

PHP example repository.

Web www.web-max.ca
Back to examples
Using FTP in PHP

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 )

<?php

// 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.
        
      
if(!file_exists("/var/www/html/testdomain/public_html/$dir"))
    {
    
// Now open the URL. The remote script can pass data back to
// us in $var

        
$fh fopen($url,"r");
        while(!
feof($fh))
        {

// Just to stop it timing out ... FTP isn't fast!
        
            
set_time_limit(10); 
            
$var fgets($fh1024);
            print(
$var);
        }
        
fclose($fh);
    }
}
?>

This is the remote file http://ftpenabled.server.com/ftp.php

<?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";

if(
$ftp=ftp_connect($ftp_ip))
{
    if(
ftp_login($ftp,$ftp_username,$ftp_password))
    {

// Set to PASV mode

        
ftp_pasv$ftp1);

// In this example set the current directory to
// public_html

        
ftp_chdir($ftp,"/public_html/");

// If we cannot set our directory to the new one
// then we create it

        
if(!ftp_chdir($ftp,$dir))
        {    
            
ftp_mkdir($ftp,$dir);
            echo(
"Directory $dir created ok");
        }
    }
    
ftp_close($ftp);
}


?>