in reply to Upload a file from home dir
As posted your code is almost unreadable. (When you post code to PM, put it between <code></code> tags. Better yet, become familiar with the contents of the PM site FAQ, especially Writeup Formatting Tips.) Rather than try reading through it, here's a simple file upload script:
Make sure that the script has permission to write to $output_file.#!/usr/bin/perl -T -- use strict; use warnings; use CGI qw(:standard); print header, start_html('Upload example'); print_form() unless param; print_results() if param; print end_html; sub print_form { print h2('What file do you have for me?'), start_multipart_form(), filefield(-name => 'upload', -size=>60), submit(-label => 'Do it!'), end_form; } sub print_results { my $output_file = '../whatever'; my $file = upload('upload'); print h2('Bummer!...') and return unless $file and open my $out, ">$output_file"; { my $data; print $out $data while read $file, $data, 1024; } close $out or print h2('Not all is well...') and return; print h1('Success!'); }
Update: Fixed s/param/upload/ in print_results; the original works but the revision uses the "recommended idiom" (see perldoc CGI for a discussion on this).
the lowliest monk
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re2: Upload a file and format txt
by MonkPaul (Friar) on Apr 01, 2005 at 15:10 UTC |