Op het moment ben ik bezig met een website waarbij ik moet checken of de ingevulde gegevens een FTP server kunnen bereiken.
Met de code:
<?php
$ftpconnect = @ftp_connect($server);
if(!$ftpconnect){
//error
}
?>
Kom je een heel eind als de gegevens kloppen, echter geven een Warning regel 1 als ze niet kloppen. Hoe is dit te voorkomen? Vaak wordt bij zulke connecties gebruikt "OR die('Connectie niet mogelijk')", alleen dit wil ik voorkomen...

Alvast Bedankt,
Frans Piter
Het hoeft ook geen verbinding te maken, het moet een error geven dat er niks is ingevuld :P. Als ik iets invul werkt het gewoon, ik wil alleen ook een input_check doen voor de ftp gegevens, lijkt mij terecht toch? Vandaar dat ik ook niet met 'or die()' wil werken. Het script moet gewoon doorgaan met de kennis dat de FTP gegevens niet kloppen; en hierop weer teruggaan naar het formulier (waar dan een opmerking komt dat de gegevens niet werken).
Doe dit eens:

<?php
if(ftp_connect($ftp_sever) === false)
{
// foutmelding hier
echo 'Fout....';
}
else
{
// wel goed
}
?>
@ Eddy, dat is het zelfde als wat ik in mijn eerste post al deed. :)

Frans, ik heb even een probeersel uit de prullenbak gevist, en dat doet volgens mij exact wat jij wil.

SFPT.php

<?php
/**
 * Simple FTP Class
 * 
 * @package SFTP
 * @name SFTP
 * @version 1.0
 * @author Shay Anderson 05.11
 * @link shayanderson.com
 * @license http://www.gnu.org/licenses/gpl.html GPL License
 * SFTP is free software and is distributed WITHOUT ANY WARRANTY
 */
final class SFTP {
	/**
	 * FTP host
	 *
	 * @var string $_host
	 */
	private $_host;

	/**
	 * FTP port
	 *
	 * @var int $_port
	 */
	private $_port = 21;

	/**
	 * FTP password
	 *
	 * @var string $_pwd
	 */
	private $_pwd;
	
	/**
	 * FTP stream
	 *
	 * @var resource $_id
	 */
	private $_stream;

	/**
	 * FTP timeout
	 *
	 * @var int $_timeout
	 */
	private $_timeout = 90;

	/**
	 * FTP user
	 *
	 * @var string $_user
	 */
	private $_user;

	/**
	 * Last error
	 *
	 * @var string $error
	 */
	public $error;

	/**
	 * FTP passive mode flag
	 *
	 * @var bool $passive
	 */
	public $passive = false;

	/**
	 * SSL-FTP connection flag
	 *
	 * @var bool $ssl
	 */
	public $ssl = false;

	/**
	 * System type of FTP server
	 *
	 * @var string $system_type
	 */
	public $system_type;

	/**
	 * Initialize connection params
	 *
	 * @param string $host
	 * @param string $user
	 * @param string $password
	 * @param int $port
	 * @param int $timeout (seconds)
	 */
	public function  __construct($host = null, $user = null, $password = null, $port = 21, $timeout = 90) {
		$this->_host = $host;
		$this->_user = $user;
		$this->_pwd = $password;
		$this->_port = (int)$port;
		$this->_timeout = (int)$timeout;
	}

	/**
	 * Auto close connection
	 */
	public function  __destruct() {
		$this->close();
	}

	/**
	 * Change currect directory on FTP server
	 *
	 * @param string $directory
	 * @return bool
	 */
	public function cd($directory = null) {
		// attempt to change directory
		if(ftp_chdir($this->_stream, $directory)) {
			// success
			return true;
		// fail
		} else {
			$this->error = "Failed to change directory to \"{$directory}\"";
			return false;
		}
	}

	/**
	 * Set file permissions
	 *
	 * @param int $permissions (ex: 0644)
	 * @param string $remote_file
	 * @return false
	 */
	public function chmod($permissions = 0, $remote_file = null) {
		// attempt chmod
		if(ftp_chmod($this->_stream, $permissions, $remote_file)) {
			// success
			return true;
		// failed
		} else {
			$this->error = "Failed to set file permissions for \"{$remote_file}\"";
			return false;
		}
	}

	/**
	 * Close FTP connection
	 */
	public function close() {
		// check for valid FTP stream
		if($this->_stream) {
			// close FTP connection
			ftp_close($this->_stream);

			// reset stream
			$this->_stream = false;
		}
	}

	/**
	 * Connect to FTP server
	 *
	 * @return bool
	 */
	public function connect() {
		// check if non-SSL connection
		if(!$this->ssl) {
			// attempt connection
			if(!$this->_stream = ftp_connect($this->_host, $this->_port, $this->_timeout)) {
				// set last error
				$this->error = "Failed to connect to {$this->_host}";
				return false;
			}
		// SSL connection
		} elseif(function_exists("ftp_ssl_connect")) {
			// attempt SSL connection
			if(!$this->_stream = ftp_ssl_connect($this->_host, $this->_port, $this->_timeout)) {
				// set last error
				$this->error = "Failed to connect to {$this->_host} (SSL connection)";
				return false;
			}
		// invalid connection type
		} else {
			$this->error = "Failed to connect to {$this->_host} (invalid connection type)";
			return false;
		}

		// attempt login
		if(ftp_login($this->_stream, $this->_user, $this->_pwd)) {
			// set passive mode
			ftp_pasv($this->_stream, (bool)$this->passive);

			// set system type
			$this->system_type = ftp_systype($this->_stream);

			// connection successful
			return true;
		// login failed
		} else {
			$this->error = "Failed to connect to {$this->_host} (login failed)";
			return false;
		}
	}

	/**
	 * Delete file on FTP server
	 *
	 * @param string $remote_file
	 * @return bool
	 */
	public function delete($remote_file = null) {
		// attempt to delete file
		if(ftp_delete($this->_stream, $remote_file)) {
			// success
			return true;
		// fail
		} else {
			$this->error = "Failed to delete file \"{$remote_file}\"";
			return false;
		}
	}

	/**
	 * Download file from server
	 *
	 * @param string $remote_file
	 * @param string $local_file
	 * @param int $mode
	 * @return bool
	 */
	public function get($remote_file = null, $local_file = null, $mode = FTP_ASCII) {
		// attempt download
		if(ftp_get($this->_stream, $local_file, $remote_file, $mode)) {
			// success
			return true;
		// download failed
		} else {
			$this->error = "Failed to download file \"{$remote_file}\"";
			return false;
		}
	}

	/**
	 * Get list of files/directories in directory
	 *
	 * @param string $directory
	 * @return array
	 */
	public function ls($directory = null) {
		$list = array();

		// attempt to get list
		if($list = ftp_nlist($this->_stream, $directory)) {
			// success
			return $list;
		// fail
		} else {
			$this->error = "Failed to get directory list";
			return array();
		}
	}

	/**
	 * Create directory on FTP server
	 *
	 * @param string $directory
	 * @return bool
	 */
	public function mkdir($directory = null) {
		// attempt to create dir
		if(ftp_mkdir($this->_stream, $directory)) {
			// success
			return true;
		// fail
		} else {
			$this->error = "Failed to create directory \"{$directory}\"";
			return false;
		}
	}

	/**
	 * Upload file to server
	 *
	 * @param string $local_path
	 * @param string $remote_file_path
	 * @param int $mode
	 * @return bool
	 */
	public function put($local_file = null, $remote_file = null, $mode = FTP_BINARY) {
		// attempt to upload file
		if(ftp_put($this->_stream, $remote_file, $local_file, $mode)) {
			// success
			return true;
		// upload failed
		} else {
			$this->error = "Failed to upload file \"{$local_file}\"";
			return false;
		}
	}

	/**
	 * Get current directory
	 *
	 * @return string
	 */
	public function pwd() {
		return ftp_pwd($this->_stream);
	}

	/**
	 * Rename file on FTP server
	 *
	 * @param string $old_name
	 * @param string $new_name
	 * @return bool
	 */
	public function rename($old_name = null, $new_name = null) {
		// attempt rename
		if(ftp_rename($this->_stream, $old_name, $new_name)) {
			// success
			return true;
		// fail
		} else {
			$this->error = "Failed to rename file \"{$old_name}\"";
			return false;
		}
	}

	/**
	 * Remove directory on FTP server
	 *
	 * @param string $directory
	 * @return bool
	 */
	public function rmdir($directory = null) {
		// attempt remove dir
		if(ftp_rmdir($this->_stream, $directory)) {
			// success
			return true;
		// fail
		} else {
			$this->error = "Failed to remove directory \"{$directory}\"";
			return false;
		}
	}
}
?>

index.php

<?php
session_start();
require 'SFTP.php';
if( $_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['host'], $_POST['username'], $_POST['password']))
{
	$aErrors = array();

	if ( !isset($_POST['host']) or !preg_match( '~^[\w ]{3,}$~', $_POST['host'] ) ) {
    	$aErrors['host'] = 'De server klopt niet.';
  	}

	if ( !isset($_POST['username']) or !preg_match( '~^[\w ]{3,}$~', $_POST['username'] ) ) {
    	$aErrors['username'] = 'Uw gebruikersnaam klopt niet';
  	}

  	if ( !isset($_POST['password']) or !preg_match( '~^[\w ]{3,}$~', $_POST['password'] ) ) {
    	$aErrors['password'] = 'Uw wachtwoord klopt niet.';
  	}

  	elseif( $ftp = new SFTP($_POST['host'], $_POST['username'], $_POST['password'] ) )
    {
    	if(!@$ftp->connect())
    	{
    		$aErrors['server'] = 'Connection failed: '. $ftp->error; 
    	}
    }
    


  	if ( count($aErrors) == 0 ) {
    
    $_SESSION['host'] = $_POST['host'];
    $_SESSION['username'] = $_POST['username'];
    $_SESSION['password'] = $_POST['password'];
    $_SESSION['is_loggedin'] = true;
    
    //  Volgende pagina aub
    header('Location: server.php');
    exit();
  }
  
	
}
?>

<!DOCTYPE html>
 <html lang="nl">
  <head>
    <meta charset="UTF-8" />
    <title>FTP login</title>
   <script src="http://code.jquery.com/jquery-latest.min.js"></script>    
  </head>
  <style type="text/css">
      .errorlist, .error input{
        border: 1px solid #f00;
        background: #fdd;
      }
      form.cmxform fieldset {
        margin-bottom: 10px;
      }
      form.cmxform legend {
        padding: 0 2px;
        font-weight: bold;
      }
      form.cmxform label {
        display: inline-block;
        line-height: 1.8;
        vertical-align: top;
      }
      form.cmxform fieldset ol {
        margin: 0;
        padding: 0;
      }
      form.cmxform fieldset li {
        list-style: none;
        padding: 5px;
        margin: 0;
      }
      form.cmxform em {
        font-weight: bold;
        font-style: normal;
        color: #f00;
      }
      form.cmxform label {
        width: 120px; /* Width of labels */
      }
    </style>
  <body>
  	<h1>Inloggen op de server</h1>

  	 <?php
      if ( isset($aErrors) and count($aErrors) > 0 ) {
        print '<ul class="errorlist">';
        foreach ( $aErrors as $error ) {
          print '<li>' . $error . '</li>';
        }
        print '</ul>';
      }
      ?>

<form action="" method="post" class="cmxform">
<fieldset>
        <legend>Uw login gegevens..</legend>
		<ol>

			<?php echo isset($aErrors['host']) ? '<li class="error">' : '<li>' ?>
		        <label for="host">host<em>*</em></label>
		        <input id="host" name="host" />
		      </li>
		       	<?php echo isset($aErrors['username']) ? '<li class="error">' : '<li>' ?>
		        <label for="username">username<em>*</em></label>
		        <input id="username" name="username" />
		      </li>
		      <?php echo isset($aErrors['password']) ? '<li class="error">' : '<li>' ?>
		        <label for="password">password<em>*</em></label>
		        <input id="password" name="password" />
		      </li>
		</ol>
 	<input type="submit" value="login" />
</fieldset>
</form>

  </body>
</html>
@ Bart V: Dat ziet er zeer strak uit! Ik zal er vandaag even beter naar kijken of het voor mij werkt! In ieder geval ontzettend bedankt!
let wel, het is uit de prullenbak gevist.
server.php en sftp.php ben ik vrijwel zeker van dat het werkt, index.php niet helemaal.
Ik heb het opgelost. Ik heb het voor dit kleine stukje code gewijzigd, zodat geen warnings meer vertoont.
De FTP class werkt op zich prima, echter verhielp het niet mijn probleem. De error werd gewoon verplaatst naar de regel in de class met foutmelding "ftp_login(): Invalid login".

In ieder geval, ontzettend bedankt voor de hulp!

Reageren