How to upload and open a file in xampp apache server using CGI Perl Script? -


this code works fine in local xampp apache server. run same code in local area network different ip addressed system. file cannot open , cant write expected directory. kindly needful? in advance.

am passing xml file through below code.

#!"c:\xampp\perl\bin\perl.exe" #!"172.18.5.23:\xampp\perl\bin\perl.exe" #!\usr\bin\perl -wt #!perl use strict; use warnings; use cgi; $query = new cgi; print $query->header( "text/html" ); print <<end_here;    <html>       <head>         <title>my first cgi script</title>       </head>       <body bgcolor="#ffffcc">         <h1>welcome perl cgi</h1>         <form action="/cgi-bin/inputxml.cgi" method="post"         enctype="multipart/form-data">         <p>files upload: <input type="file" name="xml" /></p>         <p><input type="submit" name="submit" value="submit form" /></p>         </form>       </body>     </html>     end_here 

sending xml file below code.....

#!"c:\xampp\perl\bin\perl.exe"     #!"172.18.5.23:\xampp\perl\bin\perl.exe"     #!\usr\bin\perl -wt     #!perl     use strict;     use cgi;     use cwd 'abs_path';     use cgi::carp qw(warningstobrowser fatalstobrowser);     use file::basename;     $cgi::post_max;     $safe_filename_characters = "a-za-z0-9_.-";     $query = new cgi;         $cgi = new cgi;         $file = $cgi->param('xml');         $lines;         open(data,"<$file") or die "can't open data";         print $query->header ( );         $lines = <data>;         close(data);         $lines =~s{darling}{cgi}ig;         print $lines;         print abs_path($file);         open(out, '>dirname($file)."\\out_".basename($file)');         print out $lines;         close(out);           print $query->header ( );     print <<end_here; 

getting filename input parameter ("xml" in case) bad idea. browsers differ in behaviour. give full filename, give basename. when give full path, path on client computer - path pretty guaranteed not exist on server. [update: , re-reading question, realise that's why works when you're testing locally.]

there's detailed explanation of how this in documentation cgi module. should read of section before writing code. in summary.

# filename may include path , # should never used filename on server. $filename = $cgi->param('xml');  # try calculate local filename $local_fn; if ($filename =~ /([-\w\.]+)$/) {   $local_fn = $1; }  # file handle uploaded file $local_in_fh = $cgi->upload('xml');  # open file handle store file open $local_out_fh, '>', "/path/to/output/dir/$local_fn" or die $!;  while (<$local_in_fh>) {   # process 1 line input file (which in $_)   print $local_out_fh; } 

there couple of other things might useful you.

$cgi->uploadinfo($filename)->{'content-type'} give mime type of uploaded file. can use work out how want process file.

$cgi->tmpfilename($filename) give path temp file data has been uploaded. deleted when cgi program exits. if want save file without processing in way, can move file new location on server.

some other notes existing solution:

  1. your first program displays html file. why not have static html file?
  2. $cgi::post_max nothing.
  3. please use cgi->new instead of new cgi.
  4. you create 2 cgi objects ($query , $cgi). need one.
  5. you print cgi header twice.
  6. data special file handle name. shouldn't use own file handles.
  7. $lines = <data> first line file.
  8. open(out, '>dirname($file)."\\out_".basename($file)') isn't doing think it's doing.

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -