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 9 of 9
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    Conwy. UK
    Posts
    122
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Uploading a local file

    Hi, I'm really stuck on this.

    I want to be able to upload a local file (.pdf), put the contents into a variable and then attach it to an email. The local file name and location is known (derived from data from a database) so I don't want to use the input type of 'file' and have to browse for it (when I do it works perfectly so there are no coding issues), once I have the uploaded data there are no problems attaching it to an email etc.

    I have been using CGI.pm and I have tried to 'open' the file

    The problem I have is that I need to translate the local path and file name (C:/dir/dir/filename.ext) from an input type of 'text' into something that can be used in the $file var by read used as below
    Code:
     while ($bytesread=read($file,$buffer,1024)) {
      $filedata.=$buffer;
     }
    This seems to have taken over my life, any assistance would be fantastic.
    An answer needs a question just as much as a question needs an answer. Deep eh!

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Jeepers View Post
    Hi, I'm really stuck on this.

    I want to be able to upload a local file (.pdf), put the contents into a variable and then attach it to an email. The local file name and location is known (derived from data from a database) so I don't want to use the input type of 'file' and have to browse for it (when I do it works perfectly so there are no coding issues), once I have the uploaded data there are no problems attaching it to an email etc.

    I have been using CGI.pm and I have tried to 'open' the file

    The problem I have is that I need to translate the local path and file name (C:/dir/dir/filename.ext) from an input type of 'text' into something that can be used in the $file var by read used as below
    Code:
     while ($bytesread=read($file,$buffer,1024)) {
      $filedata.=$buffer;
     }
    This seems to have taken over my life, any assistance would be fantastic.
    i don't understand, C:/dir/dir/filename.ext is the input file or output file?

    from Cwd man page:

    working dir:
    Code:
    use Cwd;
    my $dir = getcwd;
    or absolute of a file:
    Code:
    use Cwd 'abs_path';
    my $dir = abs_path($file);
    you can get the root directory of a server using $ENV{'DOCUMENT_ROOT'}

    Edit: $file is the handle of an open statement

    best regards
    Last edited by oesxyl; 02-18-2011 at 05:12 PM.

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    Conwy. UK
    Posts
    122
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The C:/dir/dir/filename.ext is the local file. There is no output file as it is not saved to the server.

    Why I need to do this: I produce .pdf files (that have been ordered via my on-line perl coded system) for customers on a computer in the office and are saved on that computer. Via the admin panel of the on-line system I wish to get the local file and save it to a variable that will be attached to an email (all of which the system does automatically once the file has been retrieved). As the file name is derived from the original order details there is no need for the browse button etc (from the <input type="file" ...>) but I do need to convert a text string of the local file path and name into a file handle ($file) so that I can 'read' it in. The only problem I have is the conversion of the string into a file handle.
    An answer needs a question just as much as a question needs an answer. Deep eh!

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Jeepers View Post
    The C:/dir/dir/filename.ext is the local file. There is no output file as it is not saved to the server.

    Why I need to do this: I produce .pdf files (that have been ordered via my on-line perl coded system) for customers on a computer in the office and are saved on that computer. Via the admin panel of the on-line system I wish to get the local file and save it to a variable that will be attached to an email (all of which the system does automatically once the file has been retrieved). As the file name is derived from the original order details there is no need for the browse button etc (from the <input type="file" ...>) but I do need to convert a text string of the local file path and name into a file handle ($file) so that I can 'read' it in. The only problem I have is the conversion of the string into a file handle.
    this solve your problem?
    Code:
    my $file;
    open($file,"<filename") or die $!.
    but i don't understand why you want to do this because when you send the mail you can give to the mailer only the path to the attached file. Like this for example:

    http://www.perlmonks.org/?node_id=19430

    best regards

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Location
    Conwy. UK
    Posts
    122
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Your suggestion regarding emailing the attachment via the the path to it means it would have to be on the server, the file is not on the server it is on a local hard drive, we would still have the problem of transferring them to the server (without doing it manually).

    The system we have in place works (with the exception of retrieving a local file automatically), the reason we need it is that there are many other functions that happen (sending an SMS, updating the on-line historical record etc etc) and this is a key part.

    All I need to know is how to turn a text string with the path to a local file into a file handle that can be used with 'read' and is able to upload from a local machine. I can open, close, read & write files on the server, but when I use that method to access local files I get a 'does not exist' error.

    All the best
    An answer needs a question just as much as a question needs an answer. Deep eh!

  • #6
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Jeepers View Post
    Your suggestion regarding emailing the attachment via the the path to it means it would have to be on the server, the file is not on the server it is on a local hard drive, we would still have the problem of transferring them to the server (without doing it manually).

    The system we have in place works (with the exception of retrieving a local file automatically), the reason we need it is that there are many other functions that happen (sending an SMS, updating the on-line historical record etc etc) and this is a key part.

    All I need to know is how to turn a text string with the path to a local file into a file handle that can be used with 'read' and is able to upload from a local machine. I can open, close, read & write files on the server, but when I use that method to access local files I get a 'does not exist' error.

    All the best
    can be:
    - the path, try to use the Cwd module, he know how to deal with windows path.
    - check the script read/write permisons, group.
    - i guess we can exclude this but just in case check if the file realy exists.

    best regards

  • #7
    Regular Coder
    Join Date
    Jun 2002
    Location
    Conwy. UK
    Posts
    122
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the Cwd suggestion but all that does is give you the ability to manipulate server paths.

    I'm sorry if I'm not being as clear as I should be; the problem I have is not related to the web server at all it relates to a computer I have in the office in front of me (for this purpose I'll call it Graham's computer). I need to get a file from Graham's computer and via an on-line admin system (on the server) load it into a perl variable, everything after that point is working fine.

    The file on Graham's computer will have a variety of different names all of which are held in a database, the actual path and filename is passed to the sub routine as a text string (something like 'C:/customer/pdfs/john.pdf'), I need to be able to convert that text string into a file handle ($file) that can be used by the 'read' statement below. Using 'open' just opens a file on the server.

    This is the code I'm using to test what I'm doing; initially it displays the form from which you select the local file you want to upload (which is the bit I want to replace with the file path and name text string), the form then calls the script which then uploads the file. Upload it to your own site to see what it does.

    Code:
    #!/usr/bin/perl
    
    use CGI::Carp qw(fatalsToBrowser);
    
    $| = 1;
    
    use CGI qw(:cgi-lib);
    
    #parse incoming form info.
    ReadParse(*data);
    my $web = $data{'CGI'};
    
    print "Content-type: text/html\n\n";
    
    unless ($data{'initial'}){ #this is set by the form input 'initial' so the form is produced first
    # gets file name and sets 'initial' to 'Upload'
    
    my $scripturl = $web->url;
    
    my $selectFile=qq~
    <H3 ALIGN="center">Use the button below to upload the file.</H3>
    <form method="post" action="$scripturl" enctype="multipart/form-data">
    <CENTER>
    <BR>
    <input type="file" name="file" size="40" maxlength="80" />
    <input type="submit" name="initial" value="Upload" />
    </FORM>~;
    print $selectFile;
    exit;
    }
    
    my $filedata="";
    
    # everything above is just to get a filename and path and will not be part of the 'live' code
    # the line below is what I need to replace with the file handle ($file) derived from a text string but I don't know how the input type 'file' achieves this. 
    
    if ($file = $web->param("file")){ 
     while ($bytesread=read($file,$buffer,1024)) {
      $filedata.=$buffer;
     }
     close $file;
    }
    
    print $filedata; # just to show that the import has happened
    exit;
    Hopefully I've been able to explain it a little clearer this time.

    Thanks for your help.
    An answer needs a question just as much as a question needs an answer. Deep eh!

  • #8
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,871
    Thanks
    2
    Thanked 164 Times in 159 Posts
    Before I address your question, let me point out a few things.

    1) Every Perl script you write should include the strict and warnings pragmas.

    2) Don't use :cgi-lib and its accompanying ReadParse() function. They are available to maintain backwards compatibility with old scripts, but should not be used in newer scripts.

    3) The use of CGI::Carp is good while developing/debugging, but normally should be removed in production scripts. Also, enable warningsToBrowser or raise warnings to fatal.

    So, the beginning of your script should be more like this:
    Code:
    #!/usr/bin/perl
    
    use strict;
    use warnings;  # or use warnings FATAL => 'all';
    use CGI;
    use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
    
    $| = 1;
    
    my $cgi  = CGI->new;
    my %form = $cgi->Vars;
    
    print $cgi->header;
    warningsToBrowser(1);
    If I understand you correctly, the file you want to attach to the email will not be on the computer the end user is using to submit the form, but instead is on a different computer that is on the same network as the web server. So, there are 3 computers involved and you want the end user to specify the filename, but not have to navigate to it as is normally done with a file upload form field.

    If that is correct, then instead of using a field type of 'file', you'd use a 'text' field. You would then use that value when accessing the remote file by what ever connection means you have available, such as ftp or scp or possibly unc path.

  • #9
    Regular Coder
    Join Date
    Jun 2002
    Location
    Conwy. UK
    Posts
    122
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for your reply. The script I've uploaded is one I've picked up to use in testing only, once integrated into the main system only the business end of the code will be used.

    This is part of the webmasters admin for the site, the customer never sees it and has no access to it. There are only two computers involved, the web sever that has the perl script and the webmasters 'home' local computer on which the form is displayed and has the PDF file on it. The sequence of events is: the PDF file is produced on or transferred to the webmasters local computer, he then logs into the admin section of the website, selects the customer from the database to whom the email needs to be sent. At this point I need the script to automatically upload the PDF file (the file name and location on the local computer is held in the database record) from the webmasters 'home' local computer to a variable and then attach it to an email and a variety of other things as well. I know that there are a whole number of ways of getting a file from a local computer to the server; all of which involve some degree of manual input or selection, the aim here is to automate the whole process.

    The thing I was looking for was how to convert a text string into a file handle suitable for use with the 'file' type of input. However, after much research I've found out that the 'file' type of input needs the submit button to be clicked once there is a file path and name in the box so that, as part of the form data submitted, the file contents are included, once received by the script this data is then extracted. At no time does the script read the original data from the users computer so it would not be possible to create a file handle manually. I can understand why; it would be a tremendous security risk if a programmer could pre-set a script to upload files from a users computer effectively without their knowledge, it is only right that the user must manually select what they want uploaded. Not very good for my idea but completely correct.

    The solution I have now come up with is that the file path and name is displayed within the form, it's copied and pasted to the 'file' input box and submitted, the script saves the file contents to a temporary file on the server, once all the files required have been uploaded in this manner the script then creates the email, attaches all the files, sends the email and then deletes all the temp files. Not quite as steam-lined as I had hoped but it does work.

    Thanks all for your suggestions.
    An answer needs a question just as much as a question needs an answer. Deep eh!


  •  

    Posting Permissions

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