PHP Class Function and Public Variable Help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Bon Bon
Forum Commoner
Posts: 66
Joined: Sat Mar 13, 2004 10:21 pm
Location: UK

PHP Class Function and Public Variable Help

Post by Bon Bon »

I have not been here for a while but I have been busy with college so I have not really had the time to check out this place.

I have just swtiched from Apcache to IIS so that I could use ASP.NET with applications that I plan to create in the future.

Getting to the point, I am writing a class in PHP and it does not seem to work properly, I do not know if it is my programming or to do with IIS.

Code: Select all

function OpenConnection () {
    $Connection = ftp_connect($IpAddress);
    ftp_login($Connection,$Username, $Password);
  }

  function ListFiles ($Connection) {
    ftp_pwd($Connection);
  }

  function CloseConnection ($Connection) {
    ftp_close($Connection);
  }
The problem is, no matter if I use $this->Connection for ListFiles() or CloseConnection() I get an error message saying:
Warning: ftp_pwd() expects parameter 1 to be resource, null given in ftp_class.php

I want to be able to use variables set in the first function in the next two functions but I cannot seem to do this. I would be greatful for any help.
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

It says in the manual that ftp_pwd returns the current directory or FALSE on error. Perhaps false is infact being returned.
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Also, if that is supposed to be a class then you are going the wrong way about defining your functions.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

class whatever
{
  var $Connection;

  function OpenConnection()
  {
    $this->$Connection = ftp_connect(...);
    ...
  }

  function ListFiles()
  {
    ftp_pwd( $this->Connection );
  }
}
Bon Bon
Forum Commoner
Posts: 66
Joined: Sat Mar 13, 2004 10:21 pm
Location: UK

Post by Bon Bon »

I tried that before and it never worked, it is still not working. I presume this is an error with IIS or something because previous work with classes which were programmed just like you have typed there seemed to work. The help is much appricated.

This is the full script so far:

Code: Select all

<?php

/**
 *   +-------------------------------------------------------------------------------------+
 *       Ftprotocol
 *   +-------------------------------------------------------------------------------------+
 *       This script has a public domain license
 *   +-------------------------------------------------------------------------------------+
 *       This script comes as-is and may harm your computer. By using this script you
 *       are liable for the outcome
 *   +-------------------------------------------------------------------------------------+
 *       Author(s): Matthew "Bon Bon" Bonner
 *   +-------------------------------------------------------------------------------------+
 *       Variables not in a class should have names with words separated by underscores
 *       and variables in a class should have words with camel-type naming
 *   +-------------------------------------------------------------------------------------+
 */

class FtpConnection {
  var $Connection;

  function OpenConnection () {
    $this->$Connection = ftp_connect('80.4.61.62');
    ftp_login($Connection, 'Anonymous', '********');
  }

  function ListFiles () {
    ftp_pwd($this->Connection);
  }

  function CloseConnection () {
    ftp_close($this->Connection);
  }
}

?>
and this is the full error I am getting:
Warning: ftp_login() expects parameter 1 to be resource, null given in C:\Documents and Settings\All Users\Documents\Web Server\includes\ftp_class.php on line 24

Warning: ftp_pwd() expects parameter 1 to be resource, null given in C:\Documents and Settings\All Users\Documents\Web Server\includes\ftp_class.php on line 28

Warning: ftp_close() expects parameter 1 to be resource, null given in C:\Documents and Settings\All Users\Documents\Web Server\includes\ftp_class.php on line 32
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

your call to ftp_login is wrong... you need to use the correct variable... (I accidently made a slight boo-boo in my posted code.. the $ before the first Connection)
Bon Bon
Forum Commoner
Posts: 66
Joined: Sat Mar 13, 2004 10:21 pm
Location: UK

Post by Bon Bon »

I still get errors, even when I use:

Code: Select all

$this-&gt;Connection = ftp_connect('80.4.61.62');
The errors I am now getting are:
Warning: ftp_login() expects parameter 1 to be resource, null given in C:\Documents and Settings\All Users\Documents\Web Server\includes\ftp_class.php on line 24

Warning: ftp_pwd(): Please log in with USER and PASS first. in C:\Documents and Settings\All Users\Documents\Web Server\includes\ftp_class.php on line 28
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

ftp_login($this->Connection, 'Anonymous', '********');
Bon Bon
Forum Commoner
Posts: 66
Joined: Sat Mar 13, 2004 10:21 pm
Location: UK

Post by Bon Bon »

Ah ha, right. I have got it working now. I see what I was doing wrong and I understand what you were talking about now. Thanks.

Code: Select all

<?php

/**
 *   +-------------------------------------------------------------------------------------+
 *       Ftprotocol
 *   +-------------------------------------------------------------------------------------+
 *       This script has a public domain license
 *   +-------------------------------------------------------------------------------------+
 *       This script comes as-is and may harm your computer. By using this script you
 *       are liable for the outcome
 *   +-------------------------------------------------------------------------------------+
 *       Author(s): Matthew "Bon Bon" Bonner
 *   +-------------------------------------------------------------------------------------+
 *       Variables not in a class should have names with words separated by underscores
 *       and variables in a class should have words with camel-type naming
 *   +-------------------------------------------------------------------------------------+
 */

class FtpConnection {
  var $Connection;

  function OpenConnection () {
    $this->Connection = ftp_connect('80.4.61.62');
    ftp_login($this->Connection, 'Anonymous', '*********');
  }

  function ListFiles () {
    ftp_pwd($this->Connection);
  }

  function CloseConnection () {
    ftp_close($this->Connection);
  }
}

?>
Bon Bon
Forum Commoner
Posts: 66
Joined: Sat Mar 13, 2004 10:21 pm
Location: UK

Post by Bon Bon »

I am trying to work out what is wrong with my IIS because it is stopping me from using PhpMyAdmin due to a problem related to classes and variables which I presume will be caused by the same problem. If I cannot get my IIS working properly I will get my mate from Microsoft to come and have a look at it for me. For now I am glad that I can allow FTP connections because then I can send my work to my house from college without any messing about. Once again thanks.
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

IIS is a pain in the ass!
Bon Bon
Forum Commoner
Posts: 66
Joined: Sat Mar 13, 2004 10:21 pm
Location: UK

Post by Bon Bon »

You deleted your last post and it makes me look even more stupid now then I did before I had to ask for help.

IIS makes me lose sleep at night.
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Nobody said your stupid :D and yeah I have to agree with you mate IIS does make you loose sleep at night.
Post Reply