Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: FTP file creation

  1. #1
    Join Date
    Nov 2008
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default FTP file creation

    Hi, I have recently started to use php scripts, and so far I have a basic (however working) login and registration system, which adds records to a MySql database. I am now trying to expand on this, and add some php code to my registration page which creates a file on my ftp account when a user submits their details. In other words, I would like a new "profile page" to be created when a valid new member registers to my site.

    So far, I have the following, but it does not work. Any suggestions?

    PHP Code:
    <?php

    function ftp_copy($source_file$destination_file)
    {
        
    $ftp_server $ftpserver;
        
    $ftp_user $ftpuser;
        
    $ftp_password $ftppass;

        
    $conn_id ftp_connect($ftp_server);
        
    $login_result ftp_login($conn_id$ftp_user$ftp_password);

        if((!
    $conn_id) || (!$login_result))
        {
                echo 
    "FTP connection has failed!";
                echo 
    "Attempted to connect to $ftp_server for user $ftp_user";
           }

        
    $fh fopen("/public_html/profiles/$userUsername","w");

    if(
    $fh==false){
    ftp_close($conn_id);
        die(
    "unable to create file");
    }
        
    ftp_close($conn_id); ?>
    Also on this script are the registration form and an "include" to my variables details, e.g. $ftp_user.

    Thanks in advance for any help.
    Last edited by AndyA; 11-27-2008 at 04:09 PM.

  2. #2
    Join Date
    Oct 2008
    Posts
    42
    Thanks
    0
    Thanked 9 Times in 9 Posts

    Default

    Judging from the code, these variables:
    Code:
    $ftpserver;
    $ftpuser;
    $ftppass;
    are defined outside of the function scope. As such, they are not visible to the function. You will need to:

    1. Pass the variables to the function (recommended)
    2. Declare them as "global" inside the function (not recommended)

  3. #3
    Join Date
    Nov 2008
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Thanks very much for the early reply. How would I go about passing the variables to the function?

    Also, the values of each variable are in another php script which I have "included" in this registration script. Why wouldn't that work?

    Thanks again.

  4. #4
    Join Date
    Oct 2008
    Posts
    42
    Thanks
    0
    Thanked 9 Times in 9 Posts

    Default

    It's a question of scope (more info). Variables used in functions are local to the function unless they are passed to the function (or declared as "global").

    To pass the variables:
    Code:
    function ftp_copy($source_file, $destination_file, $ftp_server, $ftp_user, $ftp_password)
    {
       // function code
    }

  5. The Following User Says Thank You to BabblingIdjit For This Useful Post:

    AndyA (11-26-2008)

  6. #5
    Join Date
    Nov 2008
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Thanks again John.

    The registration form and content appears fine now (without errors); however the file still isn't created.

    I am not sure, but is the specified directory valid? (As is given in my opening code):

    PHP Code:
    ("/public_html/profiles/$userUsername","w"); 
    Thanks.

  7. #6
    Join Date
    Oct 2008
    Posts
    42
    Thanks
    0
    Thanked 9 Times in 9 Posts

    Default

    Probably not. The directory is probably something more like /home/user_name_here/public_html/profiles/

    * replace user_name_here with your hosting account username

    (Of course, your host could use a different directory structure, but this is common)

    Make sure that this folder exists and is writeable.

  8. #7
    Join Date
    Nov 2008
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Hmm... the destination is writeable, but the file still isn't being created.

    I've tried all sorts of directory combinations - I guess I'll keep trying different ones for now.

    Thanks for all of your help John - you've got my main problems working great!

  9. #8
    Join Date
    Oct 2008
    Posts
    42
    Thanks
    0
    Thanked 9 Times in 9 Posts

    Default

    Assuming your hosting package include CPanel, you can use File Manager to open the folder - File Manager will display the full path to the folder. I'm sure Plesk and other control panels include a similar feature.

    If that doesn't help, post the current version of your code.

  10. #9
    Join Date
    Nov 2008
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    You're right. I am using File Manager, and the destination is shown in the form:

    <mywebsite.co.uk>/public_html/profiles

    So I tried this as the destination for fopen (as well as without the <>), but the file still wasn't created.

    I want the file created to be the username of the new member - which is why I put the end of the directory as $userUsername. I just thought that I might need to add the extension (i.e. .html). How would I correctly do this (if this works).

    (My current version of php is 5.2.6)

  11. #10
    Join Date
    Oct 2008
    Posts
    42
    Thanks
    0
    Thanked 9 Times in 9 Posts

    Default

    Most likely, you'll still need "/home/" before the rest of the path. If you can't figure it out, your host should be able to help.

    (My current version of php is 5.2.6)
    What I meant was, post the current code that you've written.

Bookmarks

Posting Permissions

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