Monday, May 28, 2007

FTP With 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 )

// 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($fh, 1024);
print(
$var);
}
fclose($fh);
}
}
?>

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”;

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

// Set to PASV mode

ftp_pasv( $ftp, 1);

// 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);
}

?>

Tuesday, May 15, 2007

Coloring Your Scrollbars with CSS

It’s true! You really can color your scrollbars and have a change of scenery from the basic gray or other browser default. It just takes a few snippets of CSS markup, which you’ll learn how to do in this tutorial.

Note: To see the results, you’ll need to view your page with Internet Explorer 5.5+, too. (Most other browsers use the browser default and do NOT support scrollbar colors - see the Further Information section below.)

You can also color the scrollbar of a form’s textarea field, as shown below. I used a variety of bright colors to show some of the scrollbar property attribute colors that are possible to change.

If you don’t see a colorful scrollbar above, here are some screen shots. (A separate popup window will open.)

Editor note Feburary 2006: Note that not everyone considers colored scrollbars a good thing, and you should also be sure the colors you choose aren’t a problem for those with color deficiencies or vision impairments. Before altering scrollbar colors, make sure they won’t be a problem to use by anyone and that they will be well received by your visitors! See the following:

This tutorial is geared for those with a knowledge of basic HTML markup, including forms, and a beginning to intermediate level knowledge of CSS.

Anton Subagja