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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Aug 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP FTP Upload Script Help

    Hey Everyone,

    I've been working on a PHP script that allows a user to upload a file from a Form via FTP to my server. The problem I'm having is that the file will not get uploaded to the server. I know that its not a Server issue because a connection is being established and I was able to see a listing in the current directory I am in on the Server. I have CHMOD the permission on the directory to 777, and I've also turned on Passive Mode in my script. But it still comes back saying that the file doesn't exist and that there was a problem uploading the file. Below is my code, any help would be appreciated.

    I put the variable $server instead of my actual HOST url, cause I don't want people accessing it, but i will change it back once I can get help on how to get this script working. PHP Code below as well as my FORM. Thanks

    PHP Code:
    <?php

    ///phpinfo();
    // FTP Configuration
    $FTP_User "$username";
    $FTP_Pass "$password";
    $FTP_Host "$server";
    $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);
        
        
    // Turns 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 directory listing
        
    print_r($contents) . "<br>"
        
        
    // 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 $FTP_Root $_FILES['File_1']['name'];
        
          
          if (
    file_exists($Filename))
          {
              echo 
    "The file $Filename exists<BR>";
          }
          else
          {
              echo 
    "The file $Filename does not exist<BR>";
          }

        
    // Set the local resource (the file that will be uploaded)
        
    $Local_Resource $_FILES['File_1']['tmp_name'];
        
        
    // If the file was successfully uploaded
        
    $upload ftp_put($Connect'$Filename''$Local_Resource'FTP_BINARY);
            
        if (!
    $upload)
        {
            
    // Show error message
            
    echo "There was a problem uploading $Local_Resource to $Filename";
        }
        else
        {
            
    // Else show success message
            
    echo "Successfully uploaded $Filename";
        }
        
    ftp_quit($Connect);
    }

    ?>
    Here is my 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>

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,027
    Thanks
    2
    Thanked 315 Times in 307 Posts
    Your form and the $_FILES['File_1'] array implement a HTTP upload, from the browser to the web server.

    I assume you are using the FTP logic to transfer this to a different server? Because, since you have already uploaded this to the web server, if the web server and the FTP server share the same file space, you should just move the file instead of going through the trouble of FTP'ing it.

    Some problems I see in the code -

    1) It assumes that register globals are on. The following line of code won't work if register globals are off (php.net has recommend for more than a few years now to not write any new code that relies on register globals being on) -
    PHP Code:
    if (isset($action) && $action == "submit"
    2) Your $Filename variable in the following is relative to the connection you have to the FTP server -
    PHP Code:
    $Filename $FTP_Root $_FILES['File_1']['name']; 
    This would have a value like - "httpdocs/charbel/upload/somefile.ext". Putting this into the file_exists() function has no meaning.

    You can use the ftp wrapper in the file_exists() function, but it would need to look like the following in order to refer to a file on the ftp server -
    Code:
    ftp://ftp.yourdomain.com/upload/somefile.ext // anonymous ftp
    
    ftp://user:password@ftp.yourdomain.com/upload/somefile.ext // non-anonymous ftp
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    New to the CF scene
    Join Date
    Aug 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    that worked great, thank you so much, take care


  •  

    Posting Permissions

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