Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Aug 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    File uploaded via ftp but size is 0kb... any help?

    I made a script that allows me to upload a file to myserver. The script works fine and uploads the file, but the file on ther server is always 0KB. The files i'm uploading are less than 1MB for testing purposes now, and I know I haven't gone passed the upload limit on the server. Any help would be appreciated.

    Below is my PHP code:
    PHP Code:
    <?php

    ///phpinfo();
    // FTP Configuration
    $FTP_User "$username";
    $FTP_Pass "$password";
    $FTP_Host "###";
    $FTP_Root "httpdocs/charbel/upload/";

    // If the form was submitted
    if (isset($action) && $action == "submit") {

        
    // Connect to the ftp address
        
    $Connect ftp_connect($FTP_Host);

        if (!
    $Connect)
        {
            echo 
    "Error: Could not connect to ftp server<br>";
            exit;
        }

            echo 
    "Connected to $FTP_Host<br>";

        
    // Login
        
    $login ftp_login($Connect$FTP_User$FTP_Pass);
        
        
    // Turn passive mode on
        
    $passive ftp_pasv ($Connecttrue );

        echo 
    "Current directory is now: " ftp_pwd($Connect) . "<br>";
        
        if (
    ftp_chdir($Connect"$FTP_Root")) 
        {
        echo 
    "Current directory is now: " ftp_pwd($Connect) . "<br>";
        }    else echo 
    "Cannot change directory"

        
    $contents ftp_nlist($Connect".");

        
    // output $contents
        
    print_r($contents); 
        
        
    // check upload status
        
    if (!passive){
            echo 
    "Failed to enter passive mode.<br>";
        }
        else {
            echo 
    "Entered passive mode.<br>";
        }

        if (!
    $login)
        {
            echo 
    "Error: Could not log on as $FTP_User<br>";
            
    ftp_quit($Connect);
            exit;
        }
        
        echo 
    "Logged in as $FTP_User<br>";

        
    // Set the filename to be uploaded
        
    $Filename $_FILES['File_1']['name'];
        
    $myFile $_Files['File_1'];
          
          if (
    file_exists($Filename))
          {
              echo 
    "The file $Filename exists<BR>";
          }
          else
          {
              echo 
    "The file $Filename does not exist<BR>";
          }
            
        
    $destination_file $FTP_ROOT.$_FILES['File_1']['name'];

        
    // Set the local resource (the file that will be uploaded)
        
    $file $myFile['tmp_name'];

        
    // If the file was successfully uploaded
        
    $upload ftp_put($Connect$destination_file$fileFTP_BINARY);
        
        if (!
    $upload)
        {
            
    // Show success message
            
    echo "There was a problem uploading $destination_file";
        }
        else
        {
            
    // Else show error message
            
    echo "Successfully uploaded $Filename";
            
        }
        
    ftp_close($Connect);
    }

    ?>
    Form Code:
    Code:
    <html>
    <head>
    <title> PHP FTP Upload Test </title>
    </head>
    <body>
    <table>
    <form method="post" action="?action=submit" enctype="multipart/form-data">
    <tr>
    <td>Username:</td>
    <td><input type="text" name="username" size="30"></td>
    </tr>
    <tr>
    <td>Password:</td>
    <td><input type="password" name="password" size="30"></td>
    </tr>
    <tr>
    <td>File:</td>
    <td><input type="file" name="File_1" size="30"></td>
    </tr>
    <tr>
    <td colspan="2" align="right"><input type="submit" value="Submit"></td>
    </tr>
    </form>
    </table>
    </body>
    </html>
    Last edited by charbsk; 08-11-2007 at 06:24 PM.

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    I made a few small changes, and it works fine for me. I removed the $myFile variable since there's no reason to have it.
    PHP Code:
    <?php

    ///phpinfo();
    // FTP Configuration
    $FTP_User $_POST['username'];
    $FTP_Pass $_POST['password'];
    $FTP_Host "yourhost.com";
    $FTP_Root "./httpdocs/";

    // If the form was submitted
    if (isset($_GET['action']) && $_GET['action'] == "submit") {

        
    // Connect to the ftp address
        
    $Connect ftp_connect($FTP_Host);

        if (!
    $Connect)
        {
            echo 
    "Error: Could not connect to ftp server<br>";
            exit;
        }

            echo 
    "Connected to $FTP_Host<br>";

        
    // Login
        
    $login ftp_login($Connect$FTP_User$FTP_Pass);
        
        
    // Turn passive mode on
        
    $passive ftp_pasv ($Connecttrue );

        echo 
    "Current directory is now: " ftp_pwd($Connect) . "<br>";
        
        if (
    ftp_chdir($Connect"$FTP_Root")) 
        {
        echo 
    "Current directory is now: " ftp_pwd($Connect) . "<br>";
        }    else echo 
    "Cannot change directory"

        
    $contents ftp_nlist($Connect".");

        
    // output $contents
        
    print_r($contents); 
        
        
    // check upload status
        
    if (!passive){
            echo 
    "Failed to enter passive mode.<br>";
        }
        else {
            echo 
    "Entered passive mode.<br>";
        }

        if (!
    $login)
        {
            echo 
    "Error: Could not log on as $FTP_User<br>";
            
    ftp_quit($Connect);
            exit;
        }
        
        echo 
    "Logged in as $FTP_User<br>";

        
    // Set the filename to be uploaded
        
    $Filename $_FILES['File_1']['name'];
          
          if (
    file_exists($Filename))
          {
              echo 
    "The file $Filename exists<BR>";
          }
          else
          {
              echo 
    "The file $Filename does not exist<BR>";
          }
            
        
    $destination_file $FTP_ROOT.$_FILES['File_1']['name'];

        
    // Set the local resource (the file that will be uploaded)
        
    $file $_FILES['File_1']['tmp_name'];

        
    // If the file was successfully uploaded
        
    $upload ftp_put($Connect$destination_file$fileFTP_BINARY);
        
        if (!
    $upload)
        {
            
    // Show success message
            
    echo "There was a problem uploading $destination_file";
        }
        else
        {
            
    // Else show error message
            
    echo "Successfully uploaded $Filename";
            
        }
        
    ftp_close($Connect);
    }

    ?>
    <html>
    <head>
    <title> PHP FTP Upload Test </title>
    </head>
    <body>
    <table>
    <form method="post" action="?action=submit" enctype="multipart/form-data">
    <tr>
    <td>Username:</td>
    <td><input type="text" name="username" size="30"></td>
    </tr>
    <tr>
    <td>Password:</td>
    <td><input type="password" name="password" size="30"></td>
    </tr>
    <tr>
    <td>File:</td>
    <td><input type="file" name="File_1" size="30"></td>
    </tr>
    <tr>
    <td colspan="2" align="right"><input type="submit" value="Submit"></td>
    </tr>
    </form>
    </table>
    </body>
    </html>
    So you're uploading a file to a server, and then transferring it to another server? Wouldn't it be easier to just upload the file to the final destination?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •