Ok, i am quite new to php and i am trying to create an application that allow two clients to access a server simultaneously. i have manged to write the code below but i keep getting errors and i think its probably because i have two form actions on the same page. please help if you can, thank you

<?php
session_start();

if (!isset($_POST['SubmitForm'])) { // if page is not submitted to itself echo the form
?>

<form action="connection.php" method="POST">
 <table>
     <tr>
      <td>FTP Server:</td>
      <td><input type="text" value="clancy.dreamhost.com" name="ftp_server"></td>
    </tr>
     <tr>
      <td>Username:</td>
      <td><input type="text" value="yusufup" name="ftp_user_name"></td>
    </tr>
    <tr>
      <td>Password:</td>
      <td><input type="password" value="yu$uf123" name="ftp_user_pass"></td>
    </tr>

     <tr>
      <td><input type="submit" name="SubmitForm" value="CONNECT"></td>
    </tr>
 </table>


</form>


<?php

} else

 {

$ftp_server =  $_POST["ftp_server"];
$ftp_user_name =  $_POST["ftp_user_name"];
$ftp_user_pass =  $_POST["ftp_user_pass"];


// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);


// check connection
if ((!$conn_id) || (!$login_result)) {
        echo "FTP connection has failed!";
        echo "Attempted to connect to $ftp_server for user $ftp_user_name";
        exit;
    } else {
    	session_start();
		session_register('ftp_user_name');
        echo "Connected to $ftp_server, for user $ftp_user_name";
        echo "<br />";
        echo "<br />";

    }
}

?>

<?php


if (!isset($_POST['Submitupload'])) { // if page is not submitted to itself echo the form
?>

<form action="<?php echo $PHP_SELF;?>"  method="POST">
 <table>

    <tr>
      <td>Local (client) File Location:</td>
      <td><input type="file" name="source_file" value="C:\Documents and Settings\admin\My Documents\uploads\sas.txt"></td>
    </tr>
    <tr>
      <td>Remote (server) File Location:</td>
      <td><input type="text" name="destination_file" value="/yusuf.smthakur.com/sas.txt"></td>
    </tr>
    <tr>
      <td><input type="submit" name="Submitupload" value="UPLOAD"></td>
    </tr>
 </table>

</form>
<?php

} else

 {
$source_file =  $_POST["source_file"];
$destination_file =  $_POST["destination_file"];

// upload the file
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);


// check if source file exist
if (!$source_file) {
        echo "Local File $source_file Does Not Exist!";
        echo "<br />";
        echo "<br />";
        exit;
    } else {
        echo "File '$source_file' Exists";
        echo "<br />";
        echo "<br />";
    }

// check if destination path exist
if (!$destination_file) {
        echo "Remote Path '$destination_file' Does Not Exist!";
        echo "<br />";
        echo "<br />";
        exit;
    } else {
        echo "Remote Path '$source_file' Exists";
        echo "<br />";
        echo "<br />";

    }

                        // check upload status
if (!$upload) {
        echo "FTP upload has failed!";
        echo "<br />";
        echo "<br />";
        $upload_link = "<a href='uploadftpform.php'>CLICK HERE</a>";
        echo "IF YOU WANT TO TRY AGAIN PLEASE $upload_link";
    } else {
        echo "Uploaded '$source_file' to '$ftp_server' as '$destination_file'";
        echo "<br />";
        echo "<br />";
        $upload_link = "<a href='uploadftpform.php'>CLICK HERE</a>";
        echo "$upload_link IF YOU WANT TO UPLOAD ANOTHER FILE";
        echo "<br />";
        echo "<br />";
        $download_link = "<a href='downloadftpform.php'>CLICK HERE</a>";
        echo " TO DOWNLOAD A FILE FROM A SERVER PLEASE $download_link";

    }

}

?>

Hi YusuMiko,

Can you tell me what is this for please. I din't understand a bit.

its an application that upload and copy files to and from server respectively. it has page has four sections. i have everything in the file above, when user login two forms are displayed, the first form is for login when user is connected he will be registered with the on the line session_register('ftp_user_name');
the problem im having is with the second form which is suppose to upload the file. when i put the forms together (login and upload) it works perfectly, but i need to separate them so that users can upload several files after they login without having to login each time. i hope i make sense.

Hei,

After moving the upload script to a different page, continue the session. Once the user logs in when the session is continued, the users logged in and he can upload multiple times without logging in for every file. I hope this helps you. I don't know how to continue the session as I'm new to php. But I read this in a tutorial.

hi,
when user login than you can save user id and password in a session variable like this

session_start();
session_register("user_id");
session_register("pass_word");
$_SESSION["user_id"] = $ftp_user_name;
$_SESSION["password"] = $ftp_user_pass;

So now you can access this variable during any page navigation until the browser closed

echo $_SESSION["user_id"];//current user id

so when you want to destroy current session variable then you need to use this function as

session_unregister("user_id");

if you want to destroy session then you need to use

session_destroy();

thanks

Hi Tulsa, i tried your way of session definition, it seems to work fine but i get an error message like this;
Notice: Undefined variable: conn_id in C:\WWW\ftp\connection.php on line 97
Warning: ftp_put() expects parameter 1 to be resoure, null was given C:\WWW\ftp\connection.php on line 97
the code on line 97 is $upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);
can you please help again, thanks

hi,

i think $conn_id variable is local so it's value destroy after refresh the page
so you need to use session variable for store this id as i posted code ok.

Thanks

listen... yusuf... you have broken the privacy rules for smthakur.com...
you have clearly stated the actual...username, hostings and password...for our servers .. and for that reason we are permanently banning you from the server..and your membership..
good day!
smthakur

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.