...

View Full Version : Script Post: Extremely Simple FTP Upload



x_goose_x
04-17-2003, 12:27 AM
This is for ConfusedOfLife, it was a bit off subject from my post, so I started a new thread...

Here's a basic ftp upload script:



<?php

$server = "127.0.0.1";
$port = 21;
$to = 120;

$usr = "john123";
$pass = "abc123";

$src = "source.php";
$dest = "destination.php";

$ftp = ftp_connect ($server, $port, $to);
ftp_login ($ftp, $usr, $pass);
ftp_put ($ftp, $dest, $src, FTP_BINARY);
ftp_close($ftp);

?>


You might also want to check out:
http://www.php.net/manual/en/ref.ftp.php

Nothing like going to the source.
When I try to connect using the dns name of my server I get an error, but if I use the IP it works, just a little warning in case it happens to you too.

ConfusedOfLife
04-17-2003, 08:29 PM
Thank you! I didn't know about ftp_connect and I'm usually so dumb that I don't search! :p

beetle
04-18-2003, 05:28 AM
If you're going to use this alot, you might like it better as a class

ftp.php
<?php

class FTP
{
function FTP( $server, $port=21, $to=120 )
{
$this->connection = ftp_connect( $server, $port, $to );
if ( !$this->connection )
{
$this->throwError( "Error connecting to FTP host" );
}
$this->mode = FTP_BINARY;
}

function login( $usr, $pass )
{
if ( !ftp_login( $this->connection, $usr, $pass ) )
{
$this->throwError( "Username or Password invalid" );
}
}

function setMode( $mode )
{
$this->mode = $mode;
}

function put( $file, $dest )
{
if ( !ftp_put( $this->connection, $dest, $file, $this->mode ) )
{
$this->throwError( "File upload not successful" );
}
}

function kill()
{
ftp_close( $this->connection );
}

function throwError( $msg )
{
die( $msg );
}
}

?>
And then, just include that sucker and interface it
<?php

include_once( "includes/ftp.php" );

$ftp = new FTP( "127.0.0.1" );
$ftp->login( "john123", "abc123" );
$ftp->put( "source.php", "destination.php" );
$ftp->kill();

?>

ConfusedOfLife
04-18-2003, 10:56 AM
Thanks beetle, you're cooooooooooooooool!

One more question, when I fsockopen, the default port is 80, and about this one it's 21. How can we find the right port if it's not 21? The same questions is true for fsockopen, what if it wasn't 80?!

beetle
04-18-2003, 01:22 PM
Hm, good question. I'll have to chew on that one a bit



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum