Solved

ftp_login with PHP

Posted on 2004-10-06
12
810 Views
Last Modified: 2013-12-12
hi,
first of all, I´m a completely newbie in PHP.

I need to be able to login to my ftp server via an web form where I only have to login with user and password. the form only has to be as simple as

<form>
<input type="text" name="user" title="user"/> User<br />
<input type="password" name="password" title="password"/>Password<br />
</form>

after login i should see the ftp surface just like I see it when I login to my ftp via the IE.

I searched and found something like that, but I´m not very into it ;)

<?php
ftp_delete($ftp, $file);    // Where $ftp is your ftp-resource
?>

Can become:
<?php
$ftp->delete($file);        // Where $ftp is your FTP-object
?>

Code:
<?php

class FTP {

   private $ftp;
   
   /* public Void __construct(): Constructor */
   public function __construct($host, $port = 21, $timeout = 90) {
       $this->ftp = ftp_connect($host, $port, $timeout);
   }
   
   /* public Void __destruct(): Destructor */
   public function __destruct() {
       @ftp_close($this->ftp);
   }

   /* public Mixed __call(): Re-route all function calls to the PHP-functions */
   public function __call($function, $arguments) {
       // Prepend the ftp resource to the arguments array
       array_unshift($arguments, $this->ftp);
       
       // Call the PHP function
       return call_user_func_array('ftp_' . $function, $arguments);
   }
   
}

?>



I hope somebody can help me with a PHP script??! Thank you very much for your immediate help!!

shed7
0
Comment
Question by:shed7
  • 5
  • 4
  • 2
12 Comments
 
LVL 33

Expert Comment

by:snoyes_jw
ID: 12238985
0
 

Author Comment

by:shed7
ID: 12239080
yeah I think the following source is close to that what I need, but only the login function without the file-upload fuction.

How does it look like in combination with a form?
sorry for such stupid questions, but.. ahem.. :)


<?php
// Herstellen der Basis-Verbindung
$conn_id = ftp_connect("$ftp_server");

// Einloggen mit Benutzername und Kennwort
$login_result = ftp_login($conn_id, "$ftp_user_name", "$ftp_user_pass");

// Verbindung überprüfen
if ((!$conn_id) || (!$login_result)) {
        echo "Ftp-Verbindung nicht hergestellt!";
        echo "Verbindung mit $ftp_server als Benutzer $ftp_user_name nicht möglich";
        die;
    } else {
        echo "Verbunden mit $ftp_server als Benutzer $ftp_user_name";
    }

// Upload der Datei
$upload = ftp_put($conn_id, "$destination_file", "$source_file", FTP_BINARY);

// Upload-Status überprüfen
if (!$upload) {
        echo "Ftp upload war fehlerhaft!";
    } else {
        echo "Datei $source_file auf $ftp_server als $destination_file geschrieben";
    }

// Schließen des FTP-Streams
ftp_quit($conn_id);
?>
0
 
LVL 33

Accepted Solution

by:
snoyes_jw earned 350 total points
ID: 12239172
First you have a web page with the form:

login.html
------------
<form action="doLogin.php" method="post">
<input type="text" name="user" title="user"/> User<br />
<input type="password" name="password" title="password"/>Password<br />
<input type="submit">
</form>

Then you have a php page with the function you posted:
doLogin.php
--------------
<?php
$ftp_user_name = $_POST['user'];
$ftp_user_pass = $_POST['password'];

// rest of php script here
0
 

Author Comment

by:shed7
ID: 12239804
then it looks like the following source, but.. where do I have to specify the ftp IP ? and.. how does it look like without the upload funtion??

<?php
$ftp_user_name = $_POST['user'];
$ftp_user_pass = $_POST['password'];

// Herstellen der Basis-Verbindung
$conn_id = ftp_connect("$ftp_server");

// Einloggen mit Benutzername und Kennwort
$login_result = ftp_login($conn_id, "$ftp_user_name", "$ftp_user_pass");

// Verbindung überprüfen
if ((!$conn_id) || (!$login_result)) {
        echo "Ftp-Verbindung nicht hergestellt!";
        echo "Verbindung mit $ftp_server als Benutzer $ftp_user_name nicht möglich";
        die;
    } else {
        echo "Verbunden mit $ftp_server als Benutzer $ftp_user_name";
    }

// Upload der Datei
$upload = ftp_put($conn_id, "$destination_file", "$source_file", FTP_BINARY);

// Upload-Status überprüfen
if (!$upload) {
        echo "Ftp upload war fehlerhaft!";
    } else {
        echo "Datei $source_file auf $ftp_server als $destination_file geschrieben";
    }

// Schließen des FTP-Streams
ftp_quit($conn_id);
?>
0
 
LVL 33

Expert Comment

by:snoyes_jw
ID: 12239860
$conn_id = "your.ftp.ip.here";

You can just delete everything from
//Upload der Datei

to just before
// Schließen des FTP-Streams

and if you want a directory listing, use the ftp_rawlist() method mentioned in the URL above.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:shed7
ID: 12242791
and what means the following error?

Fatal error: Call to undefined function: ftp_login() in /www/.../doLogin.php on line 12
0
 
LVL 10

Expert Comment

by:frugle
ID: 12243094
it means you have not defined the function ftp_login, which is strange - I thought it was a php function.

try the following code snippet on it's own.

<?php
                     
$ftp_server = "ftp.yourserver.co.whatever";
$ftp_user = "yourusername";
$ftp_pass = "yourpassword";

// set up a connection or die
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");

// try to login
if (@ftp_login($conn_id, $ftp_user, $ftp_pass)) {
   echo "Connected as $ftp_user@$ftp_server\n";
} else {
   echo "Couldn't connect as $ftp_user\n";
}

// close the connection
ftp_close($conn_id);  
?>

remember - ftp_login() issues an E_WARNING on failure.

Mike
0
 

Author Comment

by:shed7
ID: 12246465
hey mike, i typed in the ftp adress, my username and my password, but:

Fatal error: Call to undefined function: ftp_connect() in /www/.../doLogin.php on line 8


does anybody have a such a script that is allready working? maybe my server does not support such a ftp login? hmm...
0
 
LVL 10

Expert Comment

by:frugle
ID: 12246883
run this script:

<?php
phpinfo();
?>

is FTP support enabled in the FTP section? if not, you'll have to edit your php.ini or ar worst recompile php with ftp libraries.

Mike

0
 

Author Comment

by:shed7
ID: 12253447
FTP support is enabled! what do you mean with "remember - ftp_login() issues an E_WARNING on failure."

ok, once again.. i put the following code into my doLogin.php

<?php
                     
$ftp_server = $_POST['ftp.yourserver.com'];   // my host name/adress
$ftp_user_name = $_POST['user'];  // the user name that is typed in in the form of the webinterface
$ftp_user_pass = $_POST['password']; // the user password that is typed in in the form of the webinterface

// set up a connection or die
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");

// try to login
if (@ftp_login($conn_id, $ftp_user, $ftp_pass)) {
   echo "Connected as $ftp_user@$ftp_server\n";
} else {
   echo "Couldn't connect as $ftp_user\n";
}

// close the connection
ftp_close($conn_id);  
?>


then the following form into my login.html

<form action="doLogin.php" method="post">
<input type="text" name="user" title="user"/> User<br />
<input type="password" name="password" title="password"/>Password<br />
<input type="submit">
</form>



Is that the correct way? But it does not work.. :(
0
 
LVL 33

Expert Comment

by:snoyes_jw
ID: 12253512
$ftp_server = $_POST['ftp.yourserver.com'];

should be

$ftp_server = "ftp.yourserver.com";

unless you have another form field on login.html that you didn't show us
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now