Solved

ftp_login with PHP

Posted on 2004-10-06
12
809 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
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 …

705 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

17 Experts available now in Live!

Get 1:1 Help Now