Read a text file on a user's machine and write to the screen (HTML)

Ask a Question related to Perl / CGI, Design and Development.

  1. #1

    Question Read a text file on a user's machine and write to the screen (HTML)

    Hi folks, this is driving me nuts so I do hope someone can help. It seems to me that this should be simple but everything I try I get: No such file or directory

    What I want to do is read a text file from a user's machine and write the contents to a new page that I display. Here is (my not so elegant) initial script (though this will be tidied up once it works):

    # Print the file upload form
    print qq{<FORM ENCTYPE="multipart/form-data" ACTION="xxxxxx.pl"\n
    METHOD="POST"> <p>${sfont}Please select the log file you want to process:${efont}<BR>\n
    <INPUT TYPE="FILE" NAME="file"> <p> <INPUT TYPE="submit"> </FORM>} ;

    Now the relevant bits from the next script (xxxxxx.pl):

    use strict ;
    use CGI::Carp qw/warningsToBrowser fatalsToBrowser/ ;
    use DBI;
    use File::Basename;

    my $file = param('file') ;
    $file=~m/^.*(\\|\/)(.*)/;
    my $infile = upload('file');
    open(FILE, "<$infile") or die "$!" ;

    At which point is dies with the following message on screen:

    No such file or directory

    I cannot fathom it for the life of me . Before I go completely bonkers and pull the one remaining hair out, can anyone help?

    Many thanks
    Toby Corballis is offline Junior Member
    Join Date
    Nov 2010
    Posts
    2

  2. Similar Questions and Discussions

    1. Unable to read/write to .ini file using .dll in webservice
      Hi, I am new to asp.net. I am creating a web service. This I havedone. The web service calls one of our .dll's. This .dll usesthe...
    2. File system get auto change from read-write to read-oly
      I have a very strange file system with OS Redhat 7.2 The file system is read-write, but some how it randomly changes to read-only at any time....
    3. read/write binary file
      I'm writing a web service which will return a string containing the contents of a binary file (converted using System.Convert.ToBase64String). When...
    4. How do you make a fireworks html file to write text in?
      or you could slice out the middle of the square where you want to have text, export the html into dreamweaver, click on the center slice, copy the...
    5. Read the contents of a text file into an HTML page
      "Dutch3001" <mandygretahello@msn.com> wrote in message news:57c4bcf5.0307182134.78d05a86@posting.google.com... html wont do it, and client side...
  3. #2

    Default Re: Read a text file on a user's machine and write to the screen (HTML)

    Hello Toby

    I hope this helps, its a bit of code i use my self

    Code:
    <form action='../cgi-bin/update/getupdate.pl' method='post' enctype='multipart/form-data' target='updateFrame'>
    <b>Update Firmware </b><hr>
    <input type='file' name='getthisfile'><input type='submit' name='buttona' value='Update'>
    </form>
    and the actual perl bits to save the file that go into the perl script

    Code:
    use CGI ':standard';
    my $query = new CGI;
    my $upload_filehandle = $query->upload("getthisfile");
    $ps=$ps.$upload_filehandle;
    open ( UPLOADFILE, "> /mnt/img/update.pl" ) or print "\nERROR: Unable to create new file (Not uploaded) <br>\n";
    binmode UPLOADFILE;
    while (<$upload_filehandle>){
    	print UPLOADFILE;
    }
    close UPLOADFILE;
    Chris
    malcomosx is offline Junior Member
    Join Date
    Dec 2010
    Location
    Ontario
    Posts
    2

  4. #3

    Default Re: Read a text file on a user's machine and write to the screen (HTML)

    Thanks Chris. For some unknown reason I couldn't get anything to work until I stopped trying to open the file. So what ended up working was to get the name of the file from CGI, then treat it as a bytestream:

    my $infile = upload('file'); # Get the special file handle from CGI
    my $filesize = -s $infile ; # Get the file size
    # File handling varaiables
    my ($bytesread, $buffer, $str, $hex, $rec_size) ;
    $bytesread = read($infile, $buffer, $filesize) ; # Have to read the whole file at once
    # The loop round until the end of file is hit
    while ($i <= $filesize) {
    $chk = substr $buffer, $i, 1 ;
    $hex = unpack('H*', $chk) ; # Get the hex representation of the current character
    if ($hex eq '0a') { # We've hit the CRLF character so put rec into an array
    $end = $i - $start ; # Set the length of the record
    .... do stuff
    $start = $i+1 ; # Increment the start parameter
    #push (@records, $rec) ; # Get the record into the array
    }

    Odd but it worked.
    Toby Corballis is offline Junior Member
    Join Date
    Nov 2010
    Posts
    2

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139