Solved

ftp_login with PHP

Posted on 2004-10-06
12
831 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 

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
 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

622 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