Hi. I am looking to create a FTP login from my website.
It will have two fields, a
username and
pass.
My server/host connects from a broswer with 'ftp://specificuser@website.com' and locks the users into their directories.
The whole 'Username' would be 'specificuser@website.com'. That's fine.
1) The first problem is with the PHP script I found, I would have to to connect to a generic FTP address instead and this is not possible with the above scenario. I don't know how to modify the PHP code below.
2) Second problem. I'd like to take out the directory portion of the HTML. It's simple to remove but how does it relate to the PHP portion?
Can someone be kind enough to take a look at it?
Here is the HTML code referencing the ftp.php file:
HTML Code:
<form action="ftp.php/" method="POST">
<p>Username<input name="username">
<p>Password<input name="password" type="password">
<p><input name="login" type="submit" value="login">
</form>
Here is the PHP code:
PHP Code:
<?php
/*
*/
// quick "die" function
function funk_die($msg) {
if($msg=='normal') {
return "<html><body><a href=\"ftp.php\">back</a></body></html>";
}
}
// function to give dir listing stuff
function dir_funk () {
// cookie?
global $ftp;
// dir name
$dir=ftp_pwd($ftp);
// dir listing array
$dirlist=ftp_rawlist($ftp,$dir);
// print the directory listing
$funk = "<html><body><p>current directory: <b> $dir </b></p>";
$funk .= "<p><a href=\"ftp.php?&updir=$dir\">go up one level</a>
</p>";
$funk .= "<p>
<table cellspacing=0 cellpadding=2 border=1>
<tr><td><b>permissions</b></td><td><b>file/directory</b></td>
<td><b>download</b></td><td><b>delete</b></td>
<td><b>chmod</b></td><td><b>rename</b></td>
</tr>
";
while(list($i,$thingy)=each($dirlist)) {
$thingy=preg_replace("/\s+/", " ", $thingy);
if($i==0) {
echo "";
} else {
list($chmod,$thing,$owner,$group,$size,$month,$day,$time,$filename)=explode(" ",$thingy);
if(ereg("d",$chmod)) {
$funk .= "<tr><td>$chmod</td><td><a href=\"ftp.php?dir=$dir/$filename\">$filename</a></td>
<td>dir</td>
<td><a href=\"ftp.php?delete=dir&thingy=$dir/$filename\">delete</a></td>
<td><a href=\"ftp.php?chmod=1&thingy=$dir/$filename\">chmod</a></td>
<td><a href=\"ftp.php?rename=$dir/$filename\">rename</a></td>
</tr>";
} else {
$funk .= "<tr><td>$chmod</td><td>$filename</td>
<td><a href=\"ftp.php?dl=1&file=$dir/$filename\">d/l ($size)</a></td>
<td><a href=\"ftp.php?delete=file&thingy=$dir/$filename\">delete</a></td>
<td><a href=\"ftp.php?chmod=1&thingy=$dir/$filename\">chmod</a></td>
<td><a href=\"ftp.php?rename=$dir/$filename\">rename</a></td>
</tr>";
}
}
}
$funk .= "</table>
</p>";
$funk .= "<p>Create a new directory<br>
<form action=\"ftp.php\" method=\"POST\">
<input size=\"15\" name=\"name\">
<input type=\"hidden\" name=\"current_dir\" value=\"$dir\">
<input type=\"submit\" name=\"newdir\" value=\"create_dir\">
</form>
</p>";
$funk .= "<p>Upload a local file<br>
<form action=\"ftp.php\" method=\"POST\">
<input type=\"file\" name=\"localfile\"><br>
<input type=\"hidden\" name=\"current_dir\" value=\"$dir\">
<input type=\"submit\" name=\"upload\" value=\"upload_file\">
</form>
</p>";
$funk .= "</body></html>";
return $funk;
}
if(!$login) {
// print the login form
echo "<html><body><p><b>ftp login</b><hr></p>
<form action=\"ftp.php\" method=\"POST\">
<p>Your server (eg ftp.yoursite.com)<br> <input size=\"30\" name=\"server\"></p>
<p>Username <br> <input size=\"30\" name=\"username\"></p>
<p>Password <br> <input size=\"30\" name=\"password\"></p>
<p><input type=\"submit\" name=\"login\" value=\"login\"></p>
</form></body></html>";
} else {
// try to connect or die with an error
$ftp=ftp_connect($server) or die("invalid server");
// try to login or die with error
$conn=ftp_login($ftp,$username,$password) or die("login failed for some reason");
// set a cookie so the !$login form above is never displayed
setcookie("login","active");
// set goody cookies
setcookie("server",$server);
setcookie("username",$username);
setcookie("password",$password);
if($dir) {
ftp_chdir($ftp,$dir);
echo dir_funk();
} elseif($dl) {
if($get) {
if(!$local) {
die("please enter a value");
}
ftp_get($ftp, $local, $file, FTP_BINARY);
echo funk_die(normal);
} else {
echo "<html><body>";
echo "download file";
$size=ftp_size($ftp,$file);
$time=ftp_mdtm($ftp,$file);
$time=date("d M Y, H i",$time);
echo "<p><table cellspacing=0 cellpadding=2 border=1>
<tr><td><b>filename</b></td><td><b>size</b></td>
<td><b>date modified</b></td></tr>
<tr><td>$file</td><td>$size</td><td>$time</td></tr></table>";
echo "<form action=\"ftp.php\" method=\"POST\">
where to download the file to (/home/me/file.txt or c:/file.txt):<br>
<INPUT NAME=\"local\" size=\"40\">
<input type=\"hidden\" name=\"dl\" value=\"1\">
<input type=\"hidden\" name=\"file\" value=\"$file\"><br>
<INPUT TYPE=\"submit\" name=\"get\" VALUE=\"download file\">
</FORM>";
echo "</body></html>";
}
} elseif($rename) {
if($do) {
if(!$val) {
die("please enter a value");
}
ftp_rename($ftp, $rename, $val);
echo funk_die(normal);
} else {
echo "<html><body>
rename this file: $rename
<p>
<form action=\"ftp.php\" method=\"POST\">
Rename to: <input size=\"50\" name=\"val\" value=\"$rename\"><br>
<input type=\"hidden\" name=\"rename\" value=\"$rename\">
<input type=\"submit\" name=\"do\" value=\"rename\">
</form>
</body></html>";
}
} elseif($chmod) {
if($do) {
if(!$val) {
die("please enter a value");
}
$cmd="CHMOD $val $file";
ftp_site($ftp, $cmd);
echo funk_die(normal);
} else {
echo "<html><body>
chmod this file: $thingy
<p>
<form action=\"ftp.php\" method=\"POST\">
<input size=\"4\" name=\"val\" maxlength=\"4\"> CHMOD value<br>
<input type=\"hidden\" name=\"file\" value=\"$thingy\">
<input type=\"hidden\" name=\"chmod\" value=\"1\">
<input type=\"submit\" name=\"do\" value=\"chmod\">
</form>
</body></html>";
}
} elseif($delete) {
if($delete=='file') {
ftp_delete($ftp, $thingy);
} elseif($delete=='dir') {
ftp_rmdir($ftp, $thingy);
}
echo funk_die(normal);
} elseif($upload) {
if(!$localfile) { die("please enter a file"); }
$file = substr( strrchr( $localfile, "/" ), 1 );
$put_thingy = $current_dir . "/" . $file;
ftp_put($ftp, $put_thingy, $localfile, FTP_BINARY);
echo funk_die(normal);
} elseif($newdir) {
if(!$name) { die("please enter a name"); }
$newdir = $current_dir . "/" . $name;
ftp_mkdir($ftp,$newdir);
echo funk_die(normal);
} elseif($updir) {
$newdir = str_replace( substr( strrchr( $updir, "/" ), 1 ) , "", $updir);
ftp_chdir($ftp,$newdir);
echo dir_funk();
} else {
echo dir_funk();
}
}
?>
Could someone show me proper working code please?