Hallo,
ich wollt mir ein WebFTP machen, und ich habe dieses aus meinem PHP Buch abgetippselt (keine tippfehler!!)
Hier erstmal die Dateien (sorry für den vielen code, ich glaube der wird wichtig sein!)
ftp.php:
PHP-Code:
<?php
class ftp {
var $ftp_host = '127.0.0.1';
var $ftp_username = 'web1';
var $ftp_password = 'server';
var $conn = false;
var $pasv = 1;
var $temp = '/tmp/ftpclass';
}
function ftp () {
$this->conn = ftp_connect( $this->ftp_host );
ftp_login(
$this->conn,
$this->ftp_username,
$this->ftp_password );
$this->path = ftp_pwd( $this->conn );
ftp_pasv( $this->conn, $this->pasv );
}
function ls( $dir ) {
$list = ftp_rawlist ( $this->conn, $dir );
$p = '/';
// mod
$p .= '([ldps-]{1})([rwx-]{9})\s+';
// int
$p .= '([\d]+)\s+';
// user
$p .= '([\da-z-_]+)\s+';
// group
$p .= '([\da-z-_]+)\s+';
// size
$p .= '([\d]+)\s+';
// date
$p .= '(\S+\s+\S+\s+\S+)\s+';
// filename
$p .= '(.+)';
// end
$p .= '$/si';
foreach ( $list AS $entry ) {
$i++;
preg_match( $p, $entry, $match );
$r[$i]['all'] = $match[0];
$r[$i]['type'] = $match[1];
$r[$i]['mod'] = $match[2];
$r[$i]['bit'] = $match[3];
$r[$i]['user'] = $match[4];
$r[$i]['group'] = $match[5];
$r[$i]['size'] = $match[6];
$r[$i]['date'] = $match[7];
$r[$i]['file'] = $match[8];
}
return $r;
}
function cd ($dir) {
return ftp_chdir($this->conn, $dir);
}
function makedir($dir) {
return ftp_mkdir($this->conn, $dir);
}
function removedir ( $dir ) {
return ftp_rmdir( $this->conn, $dir );
}
function rm ( $file ) {
return ftp_delete( $this->conn, $file );
}
function get( $file ) {
$tmpfile = md5( microtime( ) );
$tmpfile = $this->temp.$tmpfile;
ftp_get( $this->conn, $tmpfile, $file, FTP_BINARY );
$fp = fopen ( $tmpfile, 'r');
$data = fread( $fp, filesize( $tmpfile ) );
fclose ( $fp );
unlink ( $tmpfile );
return $data;
}
function put ( $file, $sourcefile ) {
return ftp_put(
$this->conn,
$file,
$sourcefile,
FTP_BINARY
);
}
function quit () {
return ftp_quit( $this->conn );
}
?>
index.php:
PHP-Code:
<?php
function compatible ( $g) {
foreach( $_GET AS $k => $v ) {
$g[$k] = $v;
}
foreach( $_POST AS $k => $v ) {
$g[$k] = $v;
}
}
if( isset( $_GET ) ) {
compatible( &$GLOBALS );
$_upload = &$HTTP_POST_FILES;
} else {
$_upload = &$FILES;
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>FTP</TITLE>
</HEAD>
<STYLE>
td.lineone {
background-color: #dddddd;
}
td.linetwo {
background-color: #eeeeee;
}
body, table, td, tr {
font-family: Verdana, Tahoma, Helvetica;
font-size: 10pt;
}
a:link, a:active, a:visited {
color: black;
text-decoration: none;
}
a:hover {
color: black;
text-decoration: underline;
}
</STYLE>
<BODY>
<TABLE>
<TR>
<TD>
<?php
include( 'ftp.php' );
$ftp = new ftp;
function bg( $key ) {
return ($key%2)?'lineone':'linetwo';
}
function slash( $loc ) {
$r = preg_match( '/\/$/', $loc );
return ($r)?'':$loc.'/';
}
function linkstr( $loc ) {
GLOBAL $PHPSELF, $dir;
switch( $type ) {
case 'd':
return $PHP_SELF.'?dir='.urlencode( slash( $file ) );
break;
case '-':
return 'wrapper.php?file='.urlencode( $file );
break;
}
}
function backlink( $dir ) {
return substr(
$dir,
0, strrpos( substr( $dir,0,-1), '/' )
);
}
if( !isset($dir) OR $dir == '') {
$dir = '/';
}
switch ( $action ) {
case 'upload':
$r = $ftp->put($dir.$_upload['uploadfile']['name'],$_upload['uploadfile']['tmp_name']);
if( $r ) {
echo 'Fileupload erfolgreich';
} else {
echo 'Fileupload fehlgeschlagen';
}
break;
case 'del':
if( $type == 'dir' )
$r = $ftp->removedir( $dir.$file );
else
$r = $ftp->rm( $dir.$file );
if ($r) {
echo 'Erfolgreich gelöscht';
} else {
echo 'Löschen fehlgeschlagen';
}
break;
case 'makedir':
$r = $ftp->makedir( $dir.$makedir );
if ($r) {
echo 'Verzeichniss erfolgreich angelegt';
} else {
echo 'Verzeichniss konnte nicht angelegt werden.';
}
break;
}
$files = $ftp->ls ( $dir );
echo '<TABLE>';
$tdlinktpl = '<td class="%s">[url="%s"]%s[/url]</td>';
$tdtpl = '<td class="%s">%s</td>';
$str = '<tr><td colspan="6">Browsing ftp://%s@%s%s</tr>';
printf( $str, $ftp->ftp_username, $ftp->ftp_host, $dir );
$back = '<tr><td>[img]back.gif[/img]</td><td colspan="5">[url="%s"]Zurück[/url]</td></tr>';
printf( $back, linkstr( 'd', backlink( $dir ) ) );
if ( count( $files ) == 0 ) {
echo '<tr><td colspan="6">Verzeichniss leer.</td></tr>';
} else {
foreach( $files AS $key => $value ) {
echo '<tr>';
// Typen
switch( $value['type'] ) {
case 'd':
$img = '[img]folder.gif[/img]';
break;
default:
$img = '[img]text.gif[/img]';
break;
}
printf( $tdtpl, bg( $key ), $img);
}
}
printf(
$tdlinktpl,
bg( $key ),
linkstr( $value['type'],
$dir.$value['file']),
$value['file']
);
$size = sprintf ('%01.4f MB',$value['size']/1024/1024);
printf( $tdtpl, bg( $key ), $size);
printf( $tdtpl, bg( $key ), $value['date']);
$url = $PHP_SELF.'?dir='.$dir.'&file='.
urlencode( slash( $value['file'] ) ).'&action=del';
if( $value['type'] == 'd' ) $url .= '&type=dir';
printf( $tdlinktpl, bg( $key ), $url, 'del');
$url = $PHP_SELF.'?dir='.$dir.'&file='.
urlencode( slash( $value['file'] ) ).'&action=ren';
if( $value['type'] == 'd' ) $url .= '&type=dir';
printf( $tdlinktpl, bg( $key ), $url, 'ren');
echo '</tr>';
//}
echo '</table>';
//}
?>
</TD>
</TR>
</TABLE>
<HR>
<h3>Datei hochladen</h3>
<FORM ENCTYPE="multipart/form-data" ACTION="<?php echo $PHP_SELF ?>" METHOD=POST>
<INPUT TYPE="hidden" NAME="action" VALUE="upload">
<INPUT TYPE="hidden" NAME="dir" VALUE="<?php echo $dir ?>">
<INPUT NAME="uploadfile" TYPE="file">
<INPUT TYPE="submit" VALUE="Send File">
</FORM>
<HR>
<h3>Verzeichniss erstellen</h3>
<FORM ACTION="<?php echo $PHP_SELF ?>" METHOD=POST>
<INPUT TYPE="hidden" NAME="action" VALUE="makedir">
<INPUT TYPE="hidden" NAME="dir" VALUE="<?php echo $dir ?>">
<INPUT NAME="makedir" TYPE="input">
<INPUT TYPE="submit" VALUE="Create Dir">
</FORM>
<?php
$ftp->quit( );
?>
</BODY>
</HTML>
wrapper.php:
PHP-Code:
<?php
include( 'ftp.php' );
$ftp = new ftp;
$name = explode( '/', $file );
$name = $name[count($name)-1];
header("Content-disposition: filename=$name");
header("Content-Type: application/octet-stream");
echo $ftp->get( $file );
?>
Als Fehlermeldung kommt immer:
Fatal error: Call to undefined method ftp::ls() in F:\blub\bla\index.php on line 112