Issues with ftp_put() Upload

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • PehJota
    New Member
    • Apr 2007
    • 1

    Issues with ftp_put() Upload

    I'm kinda new to PHP, and I'm trying to make a simple FTP upload script. It successfully connects to the server and logs in, but it can't upload. The transfer mode I use normally is ASCII (like I have it in the script), and for now, I have the mode set to passive (although I'll give a choice later on when I actually implement the script). (I starred out the password here, it is in the file, though.)

    Here's the entire page (not much, just the script itself):

    [PHP]<?php

    $source_file='C :\cmd.txt';
    $destination_fi le='cmd.txt';

    $name='TestGuy' ;
    $type='Text Document';

    // set up basic connection
    $conn_id = ftp_connect('ft p.pehjota.com') ;

    // login with username and password
    $login_result = ftp_login($conn _id,'pehjotaupl oads','******') ;

    // check connection
    if (!$conn_id){
    echo "Connection failed!<br />";
    exit;
    } else {
    echo "Connected. <br />";
    }
    if (!$login_result ){
    echo "Login failed!<br />";
    exit;
    } else {
    echo "Logged in.<br />";
    }

    // upload the file
    $passive=ftp_pa sv($conn_id,tru e);
    $upload=ftp_put ($conn_id,$dest ination_file,$s ource_file,FTP_ ASCII);

    // check upload status
    if (!passive){
    echo 'Failed to enter passive mode.<br />';
    }
    else {
    echo 'Entered passive mode.<br />';
    }
    if (!$upload) {
    echo 'Upload failed! Could not upload "'.$source_file .'" to "http://www.pehjota.com/uploads/'.$destination_ file.'"<br />';
    //mail('random@pe hjota.com','New Submission',$na me.' has tried to submit a '.$type.'. The file is located at: http://www.pehjota.com/uploads/'.$destination_ file,'From: name@email.com' );
    } else {
    echo 'Uploaded "'.$source_file .'" to <a href="http://www.pehjota.com/uploads/'.$destination_ file.'">"http://www.pehjota.com/uploads/'.$destination_ file.'"</a><br />';
    mail('random@pe hjota.com','New Submission',$na me.' has submitted a '.$type.'. The file is located at: http://www.pehjota.com/uploads/'.$destination_ file);
    }

    // close the FTP stream
    $close=ftp_clos e($conn_id);

    if (close){
    echo 'FTP connection successfully closed.';
    }
    ?>[/PHP]

    I also tried adjusting chmod permissions for the folder to which this would upload (to 777) just in case, but that did nothing.
  • r4ccoon
    New Member
    • Jul 2007
    • 3

    #2
    FTP_upload with IIS and maybe apache

    i've been wondering this too..
    but now after some view try and error, i can solve this problem...
    first you need to specify your local file that you want to upload...
    and must with full path like c:/abc/abc.file
    don't use $_FILES[s]['filenameform'] because this will be same as moving file from server to some folder with ftp. thats not what we want...

    you can use application/x-www-form-urlencoded in enctype of the head of the form. and
    use $_post['filenameform'] to get the path from your local hard drive. you can relax if you use IE because this browser post them with full path,
    like c:\\abc\\abc.fi le. but in firefox, the browser only pass the filename only, like abc.file..this will not upload anything...
    so solution for this thing is using javascript, so after you pick a file from file form, some hidden form will catch the path..

    pasv mode is used when "It may be needed if the client is behind firewall." thats php manual says, i dont know what it means, wether the
    browser is firewalled or the php is firewalled???


    this is the source code

    =====
    =====

    <div align="left"><p re><?
    if($_POST['button']=="Submit"){
    $localfile = //"H:/666/666Satan Vol. 04.zip"; working...
    //$_FILES['f']['tmp_name']; not working...
    $_POST['f_hidden']; // working
    print_r ($_FILES); //debugging purpose
    print_r ($_POST); //debugging purpose

    $ftp_server = "localhost" ;
    $remote_file = rand().$_POST['f'];
    // set up a connection or die
    $conn_id = ftp_connect($ft p_server) or die("Couldn't connect to $ftp_server");
    $ftp_user_name= "newuser";
    $ftp_user_pass= "wampp";
    $login_result = ftp_login($conn _id, $ftp_user_name, $ftp_user_pass) ;
    //ftp_pasv($conn_ id, true); //you can comment or uncomment it if you using firewall

    echo "login result : ".$login_result ."\n";
    echo "local file ".$localfile."\ n";
    echo "remote file ".$remote_file. "\n";

    echo "Current directory is now: " . ftp_pwd($conn_i d) . "\n";
    // change directory if needed,
    if (ftp_chdir($con n_id, "/trans/files/")) {
    echo "Current directory is now: " . ftp_pwd($conn_i d) . "\n";
    } else echo "can't change dir";

    if (ftp_put(
    $conn_id,
    $remote_file,
    $localfile,
    FTP_BINARY )) {
    echo "successful ly uploaded $file\n";
    } else {
    echo "There was a problem while uploading $file\n";
    }

    // close the connection
    ftp_close($conn _id);

    }
    ?></pre></div>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitl ed Document</title>
    </head>
    <script>
    // this js function will fill f_hidden form value with full path of a local file
    function changehidden(){
    document.upload Form.f_hidden.v alue=document.u ploadForm.f.val ue;
    }
    </script>
    <body class="twoColHy bRt">

    <div id="container" >
    <div id="sidebar1">
    <h3>&nbsp;</h3>
    <!-- end #sidebar1 --></div>
    <div id="mainContent ">
    <h1> Main Content </h1>
    <form action="<?=$_SE RVER['PHP_SELF']?>" enctype="applic ation/x-www-form-urlencoded" method="post" name="uploadFor m" id="uploadForm" >
    <input type="file" name="f" id="f" onchange="chang ehidden()" on />
    <input type="hidden" name="f_hidden" value="" />
    <input type="submit" name="button" id="button" value="Submit" />
    </form>

    </div>
    </div>
    </body>
    </html>


    ====
    ====

    sorry that i dont use CODE tag, because if you copy it straightly you will copy the line numbers to
    Attached Files

    Comment

    Working...