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 13 of 13
  1. #1
    Regular Coder
    Join Date
    Jan 2011
    Posts
    136
    Thanks
    101
    Thanked 0 Times in 0 Posts

    ftp file upload using perl

    I am trying to figure out how to do ftp file uploading using perl, and below is my code. I do not know why it is not working.

    This is the html part:
    Code:
     <input type="file" name="datafile" size = "86">
    This is the perl part:
    Code:
    #!/usr/bin/perl -w
    use CGI; 
    use File::Basename;
    use Net::FTP;
    
    my $query = new CGI; 
    my $filename = $query->param("datafile");
    
    $ftp = Net::FTP->new("ftp.mysite.com", Debug => 1)
        or die "Cannot connect to hostname: $@";
    $ftp->login('username', 'password')
        or die "Cannot login ", $ftp->message;
    $ftp->put("$filename");
    $ftp->quit();

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by camaross View Post
    I am trying to figure out how to do ftp file uploading using perl, and below is my code. I do not know why it is not working.

    This is the html part:
    Code:
     <input type="file" name="datafile" size = "86">
    This is the perl part:
    Code:
    #!/usr/bin/perl -w
    use CGI; 
    use File::Basename;
    use Net::FTP;
    
    my $query = new CGI; 
    my $filename = $query->param("datafile");
    
    $ftp = Net::FTP->new("ftp.mysite.com", Debug => 1)
        or die "Cannot connect to hostname: $@";
    $ftp->login('username', 'password')
        or die "Cannot login ", $ftp->message;
    $ftp->put("$filename");
    $ftp->quit();
    check somewhere if $filename or $query->param('datafile') exists and is a valid filename.
    If you want to use a web interface, using cgi and http, to upload files, Net::FTP is not what you want, ftp and http are distinct protocols. Look for uploading files in the cgi module.

    best regards

  • Users who have thanked oesxyl for this post:

    camaross (04-07-2011)

  • #3
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    4
    Thanks
    0
    Thanked 4 Times in 4 Posts
    Does your <form> tag have this attribute?

    enctype="multipart/form-data"

    Unless you use that, the "input file" tag will only pass the name of the file to the script, not the actual file.

    I'm not too familiar with the FTP module, but I'd imagine it wouldn't like that.

    Also, in what way isn't it working? Any errors you can give us?

  • Users who have thanked 4Rum for this post:

    camaross (04-07-2011)

  • #4
    Regular Coder
    Join Date
    Jan 2011
    Posts
    136
    Thanks
    101
    Thanked 0 Times in 0 Posts
    Thank you very much for the reply. So I can not use http to call ftp to upload a file?



    Quote Originally Posted by oesxyl View Post
    check somewhere if $filename or $query->param('datafile') exists and is a valid filename.
    If you want to use a web interface, using cgi and http, to upload files, Net::FTP is not what you want, ftp and http are distinct protocols. Look for uploading files in the cgi module.

    best regards

  • #5
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by camaross View Post
    Thank you very much for the reply. So I can not use http to call ftp to upload a file?
    you can use both, is nothing wrong if this is what you want,
    can you tell what do you want to do? why do you need to mix ftp and http?

    best regards

  • #6
    Regular Coder
    Join Date
    Jan 2011
    Posts
    136
    Thanks
    101
    Thanked 0 Times in 0 Posts
    I want to enable users of my website to upload files to another ftp site that is mainly for the purpose of uploading and storing files and is much faster for uploading.

    Thank you.

    Quote Originally Posted by oesxyl View Post
    you can use both, is nothing wrong if this is what you want,
    can you tell what do you want to do? why do you need to mix ftp and http?

    best regards

  • #7
    Regular Coder
    Join Date
    Jan 2011
    Posts
    136
    Thanks
    101
    Thanked 0 Times in 0 Posts
    Thank you for the reply. After I added that, the program seems to be working hard to try to do something, but in the end, I got the same errors:

    "Global symbol "$ftp" requires explicit package name ..."



    Quote Originally Posted by 4Rum View Post
    Does your <form> tag have this attribute?

    enctype="multipart/form-data"

    Unless you use that, the "input file" tag will only pass the name of the file to the script, not the actual file.

    I'm not too familiar with the FTP module, but I'd imagine it wouldn't like that.

    Also, in what way isn't it working? Any errors you can give us?

  • #8
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by camaross View Post
    Thank you for the reply. After I added that, the program seems to be working hard to try to do something, but in the end, I got the same errors:

    "Global symbol "$ftp" requires explicit package name ..."
    are two different problems here. first is that if you upload the file using cgi you need to give up to do same thing using ftp,

    second the code:

    Code:
    #!/usr/bin/perl
    use strict;
    use warnings 'all';
    use CGI; 
    use File::Basename;
    use Net::FTP;
    Code:
    my $ftp = Net::FTP->new("ftp.mysite.com", Debug => 1)
        or die "Cannot connect to hostname: $@";
    best regards

  • Users who have thanked oesxyl for this post:

    camaross (04-07-2011)

  • #9
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    4
    Thanks
    0
    Thanked 4 Times in 4 Posts
    my $filename = $query->param("datafile");

    Needs to be

    my $filename = $query->upload("datafile");

    Make sure to run a Regex on the file upload string, if it contains any nasty characters it could cause that error.

    Beyond this I usually do the file upload from scratch.

  • Users who have thanked 4Rum for this post:

    camaross (04-07-2011)

  • #10
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by oesxyl View Post
    check somewhere if $filename or $query->param('datafile') exists and is a valid filename.
    If you want to use a web interface, using cgi and http, to upload files, Net::FTP is not what you want, ftp and http are distinct protocols. Look for uploading files in the cgi module.

    best regards
    Quote Originally Posted by 4Rum View Post
    my $filename = $query->param("datafile");

    Needs to be

    my $filename = $query->upload("datafile");

    Make sure to run a Regex on the file upload string, if it contains any nasty characters it could cause that error.

    Beyond this I usually do the file upload from scratch.
    you are aware of what this two lines are doing if you put them toghether?
    Code:
    my $filename = $query->upload("datafile");
    ....
    
    $ftp->put("$filename");
    best regards

  • Users who have thanked oesxyl for this post:

    camaross (04-07-2011)

  • #11
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    4
    Thanks
    0
    Thanked 4 Times in 4 Posts
    Quote Originally Posted by oesxyl View Post
    you are aware of what this two lines are doing if you put them toghether?
    Code:
    my $filename = $query->upload("datafile");
    ....
    
    $ftp->put("$filename");
    best regards
    Sorry I should've made it more clear that I'm not familiar with the FTP module and was recommending purely on my knowledge on how to get file data from a form.

    Though I can assure you I am referencing personal code that I know for a fact that it uploads a file to the server correctly.

    I figured if we broke the problem into parts it would be more beneficial:
    Part 1: Getting the filedata into a variable
    Part 2: Passing that data to the FTP module
    Last edited by 4Rum; 04-07-2011 at 04:32 AM. Reason: Needed more clearity

  • Users who have thanked 4Rum for this post:

    camaross (04-07-2011)

  • #12
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by 4Rum View Post
    Sorry I should've made it more clear that I'm not familiar with the FTP module and was recommending purely on my knowledge on how to get file data from a form.

    Though I can assure you I am referencing personal code that I know for a fact that it uploads a file to the server correctly.

    I figured if we broke the problem into parts it would be more beneficial:
    Part 1: Getting the filedata into a variable
    Part 2: Passing that data to the FTP module
    agree with you but op must decide if:
    - want to do upload using cgi module then he must giveup to use Net::FTP and in Part 1, he can use upload as you said then Part 2 disipear

    or

    - get the filename using cgi, as he started and do upload using Net::FTP.

    best regards

  • Users who have thanked oesxyl for this post:

    camaross (04-07-2011)

  • #13
    Regular Coder
    Join Date
    Jan 2011
    Posts
    136
    Thanks
    101
    Thanked 0 Times in 0 Posts
    Thank you all so much for the help. They are super helpful. I might decide to give up ftp.


  •  

    Posting Permissions

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