Jump to content
xisto Community
Sign in to follow this  
OwenMelbz

Edit .txt File In Ftp Via Webpage file on external ftp

Recommended Posts

Right Im new here and stuggling with a problem im having.I've currently got a Login Script to login to a external ftp and it displays the folders contents, however i need it so it echos a specific file (coursedata.cfg) into a formarea which you can then edit the file and when u click save it overwrites the file with the new one.Im really quiet getting annoyed with it xD as everything i do ends up trying to include the file from the webserver the script is hosted on and not the external ftp source.thanksFull Code Bellow

<html><head><title>Simple FTP Manager</title><style>body {font-family: Verdana, Arial, Helvetica, sans-serif;font-size: small;color: black;}a {color: #666666;text-decoration: underline;}a:hover {text-decoration: none;}</style></head><body><?php/*Simple FTP ManagerProgrammed by Federico Ram?rez ( fedekiller )Copyright 2006All Rights ReservedNOTE: You must upload the file temp.txt in the same directory than this file, and CHMOD it to 777*/$host = '';$port = 21;$timeout = 10;$user = '';$pass = '';$secure = false; // If OpenSSL support is not active, this wont work, so change it to falsefunction clean ( ){foreach ( $_REQUEST as $key => $val ){$_REQUEST [ $key ] = stripslashes ( strip_tags ( htmlspecialchars ( $val, ENT_QUOTES ) ) ) ;$$key = stripslashes ( strip_tags ( htmlspecialchars ( $val, ENT_QUOTES ) ) ) ;}}clean ( ) ;if ( $secure ){$con = ftp_ssl_connect ( $host, $port, $timeout ) ;}else{$con = ftp_connect ( $host, $port, $timeout ) ;}if ( !$con ){echo 'Could not connect to that host.';}if ( !file_exists ( 'temp.txt' ) || !is_writable ( 'temp.txt' ) ) // Checks if the temp file is there{die ( 'The temp file does not exists or is not writable' ) ;}if ( @ftp_login ( $con, $user, $pass ) ){if ( isset ( $_GET [ 'delete' ] ) ){if ( isset ( $_GET [ 'fd' ] ) ){ftp_delete ( $con, $_GET [ 'fd' ] .'/'.$_GET [ 'delete' ] ) ;}elseif ( isset ( $_GET [ 'subfd' ] ) ){ftp_delete ( $con, $_GET [ 'fd' ] .'/'.$_GET [ 'subfd' ] .'/'.$_GET [ 'delete' ] ) ;}else{ftp_delete ( $con, $_GET [ 'delete' ] ) ;}}elseif ( isset ( $_GET [ 'download' ] ) ){if ( isset ( $_GET [ 'fd' ] ) ){ftp_get ( $con, 'temp.txt', $_GET [ 'fd' ] .'/'.$_GET [ 'download' ] , FTP_BINARY ) ;}elseif ( isset ( $_GET [ 'subfd' ] ) ){ftp_get ( $con, 'temp.txt', $_GET [ 'fd' ] .'/'.$_GET [ 'subfd' ] .'/'.$_GET [ 'download' ] , FTP_BINARY ) ;}else{ftp_get ( $con, 'temp.txt', $_GET [ 'download' ] , FTP_BINARY ) ;}header ( 'Content-Disposition: attachment; filename=temp.txt' ) ; // To force the downloadreadfile ( 'temp.txt' ) ;$fp = fopen ( 'temp.txt', 'w' ) ;fwrite ( $fp, '', filesize ( 'temp.txt' ) ) ; // After we write the data in the temp file we clean itfclose ( $fp ) ;}elseif ( isset ( $_GET [ 'upload' ] ) ){if ( $_POST [ 'Submit' ] ){$file = $_FILES [ 'file' ] [ 'tmp_name' ] ;$name = $_FILES [ 'file' ] [ 'name' ] ;$path = $_POST [ 'path' ] ;$upload = ftp_nb_put ( $con, $path.'/'.$name, $file, FTP_BINARY ) ;while ( $upload == FTP_MOREDATA ){echo ".";$ret = ftp_nb_continue ( $con ) ;}if ( $upload != FTP_FINISHED ){die ( 'There has been an error uploading the file' ) ;}echo 'File uploaded!';}else{if ( isset ( $_GET [ 'subfd' ] ) ){$path = $_GET [ 'fd' ] .'/'.$_GET [ 'subfd' ] ;}elseif ( isset ( $_GET [ 'fd' ] ) ){$path = $_GET [ 'fd' ] ;}else{$path = '.';}echo '<form action="?upload" method="post" enctype="multipart/form-data"><table border="0"><tr><td>Upload a file </td><td><input type="file" name="file" /></td></tr><tr><td> </td><td><input type="hidden" name="path" value="'.$path.'" /><input type="submit" name="Submit" value="Upload!" /></td></tr></table></form>';}}else{$fd = $_GET [ 'fd' ] ;$subfd = $_GET [ 'subfd' ] ;if ( empty ( $fd ) ){$contents = ftp_nlist ( $con, '.' ) ;echo '<h1>File List: ( '.ftp_pwd ( $con ) .' ) </h1>';echo '<a href="?upload">Upload File</a><br />';echo 'Go UP<br /><br />';}elseif ( isset ( $_GET [ 'subfd' ] ) ){$contents = ftp_nlist ( $con, $fd.'/'.$subfd ) ;echo '<h1>File List: ( '.ftp_pwd ( $con ) .$fd.'/'.$subfd.'/ ) </h1>';echo '<a href="?upload&fd='.$_GET [ 'fd' ] .'&subfd='.$_GET [ 'subfd' ] .'">Upload File</a><br />';echo '<a href="?upload&fd='.$_GET [ 'fd' ] .'">Go UP</a><br /><br />';}else{$contents = ftp_nlist ( $con, $fd ) ;echo '<h1>File List: ( '.ftp_pwd ( $con ) .$fd.'/ ) </h1>';echo '<a href="?upload&fd='.$_GET [ 'fd' ] .'">Upload File</a><br />';echo '<a href="'.$_SERVER [ 'PHP_SELF' ] .'">Go UP</a><br /><br />';}$files = array ( ) ;$folders = array ( ) ;foreach ( $contents as $c ){if ( eregi ( '.', $c ) ){$files [ ] = $c;}else{$folders [ ] = $c;}}echo '<strong>Folders:</strong><br />';foreach ( $folders as $f ){if ( !empty ( $fd ) ){echo '<a href="'.$_SERVER [ 'PHP_SELF' ] .'?fd='.$fd.'&subfd='.$f.'">/'.$f.'</a><br />';}else{echo '<a href="?fd='.$f.'">/'.$f.'</a><br />';}}echo '<br /><strong>Files:</strong><br />';foreach ( $files as $f ){echo $f.' - <a href="?delete='.$f.'">Delete</a> - <a href="?download='.$f.'">Download</a><br />';//echo '<textarea name="theText" cols="150" rows="20">';include "server.cfg";//echo "</textarea>";}}echo '<div align="right">'.$host.' is powered by '.ftp_systype ( $con ) .'</div>';}else{echo 'Sorry, username and password combination does not exists';}?></body></html>

Edited by OwenMelbz (see edit history)

Share this post


Link to post
Share on other sites

Um, if I'm getting right what you want to do then take a look here

Share this post


Link to post
Share on other sites

yh but wanted a bit more than that.I've actually finished the code now and is shown below

<? // BTW IM COMMENTING EVERYTHING FOR Uecho "<title>Server Config Editor</title>";////////////////////// Teh Variables  //////////////////////$host = ''; // host - if u setup the database crap this will change according$port = 21; // ftp port, shudnt need to change$timeout = 10; // yh same aplies$user = ''; // setup database crap to set this to FTP username$pass = ''; // setup database so this is the FTP password$secure = false; // If OpenSSL support is not active, this wont work, so change it to false$con = ftp_connect ( $host, $port, $timeout ) ; //making the connect function smaller//////////////////////////////////////////////if ( isset ( $_GET [ 'copy' ] ) ){//////////////////////////////////////////////////// PART ONE - Teh Legend Of Teh TempZ0R Filez  /////////////////////////////////////////////////////$theText = $_POST["tehconfig"]; // name of the config edit field thing$theText = stripslashes($theText); // removes crap so form dnt *BLEEP*up$data = fopen("temp.txt", "w"); // opening the temp filefwrite($data,$theText); // putting the crap inside itfclose($data); // closing the whole thingecho "<script type=\"text/javascript\"> function delayer(){ window.location = \"?process\" } </script><body onLoad=\"setTimeout('delayer()', 3000)\">Processing Server Config This May Take A Few Seconds...</body>";}elseif ( isset ( $_GET [ 'process' ] ) ){//////////////////////////////////////////// PART TWO - TEH UPLOADING OF TEMPZOR /////////////////////////////////////////////function clean ( ){ //making suer no left oversforeach ( $_REQUEST as $key => $val ){ // looking around$_REQUEST [ $key ] = stripslashes ( strip_tags ( htmlspecialchars ( $val, ENT_QUOTES ) ) ) ;// converting the *BLEEP*$$key = stripslashes ( strip_tags ( htmlspecialchars ( $val, ENT_QUOTES ) ) ) ;}} // making it smallerclean ( ) ; //finish the cleaningif ( @ftp_login ( $con, $user, $pass ) ) {// if login to ftp actually works$path = "./"; //might need to play with for differnt games$upload = ftp_nb_put ( $con, "server.cfg", "temp.txt", FTP_BINARY ) ; //uploading the temp file to ftp and renaming it to server.cfgwhile ( $upload == FTP_MOREDATA ) {//while teh upload is going play around with the left over dataecho "."; // <-- dno why$ret = ftp_nb_continue ( $con ) ;}// carry on the ftp connectionif ( $upload != FTP_FINISHED ) {//onces the ftp proccess has donedie ( '=/ file didnt upload- go bug Lee' ) ;} //if it failed say thisecho 'Server Config Updated<p>'; }// once it worked say thiselse {echo 'LAWLS DIDNT LOGIN RIGHT'; }// finished part of the login if it didnt login say thatecho "<a href='index.php'>BACK</a>"; }// just a link back a pageelse{echo '<form method="post" action="?copy"><textarea name="tehconfig" cols="70" rows="20">'?><?php include ("http://forums.xisto.com/no_longer_exists/;); ?><? echo'</textarea> <br><input type="submit" name="submit" value="Update Config"></form>';}////////////////////////////////////////////////////// Teh End - I think it will be worth a tennner xD /////////////////////////////////////////////////////?>

:D:(:unsure:

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.