?
Solved

upload file

Posted on 2006-04-21
10
Medium Priority
?
176 Views
Last Modified: 2013-12-25
in my cgi script, i have small part of html code, <input type = file> where user specifies file name

1) how can i upload user's .doc file and copy it on my server.
2) finally can i copy the same file on client machine
0
Comment
Question by:perlperl
  • 6
  • 3
10 Comments
 
LVL 51

Accepted Solution

by:
ahoffmann earned 2000 total points
ID: 16512818
1) you need a cgi which reads the uploaded data and saves it to a file
2) I guess you can, but this has nothing to do with web servers, CGI or internet at all
0
 

Author Comment

by:perlperl
ID: 16525018
can you please tell me how to do that. i am using CGI/Perl
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 16525660
1) something like:

use CGI:
my $q = new CGI;
my $bytesread       = 0;
my $buffer          = '';
open( FID, '>', "/path/to/file" ) or die "cannot open file";
while (my $more=read( $q->param{'file'}, $buffer, 8192 )) { $bytesread+=$more; print FID $buffer; }
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:perlperl
ID: 16525817
i tried

#!c:/perl/bin/Perl.exe
use CGI;

print "Content-type: text/html; charset=iso-8859-1\n\n";
$|=1;

my $q = new CGI;
my $bytesread       = 0;
my $buffer          = '';
$file = $q->param('upload_file');

print "$file";

open( FID, "> $file" ) or die "cannot open file";

while (my $more=read( $file, $buffer, 8192 ))
{
      $bytesread+=$more;
      print FID $buffer;
      }

it creates file c:\\test.doc on server but it is not copying the contents of test.doc form clients machine to server.
the file is empty on server may be because its not going in whike loop
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 16525939
> open( FID, "> $file" )
dooh, that's dangereous, don't use it this way, hardcode the filename in your CGI.

> .  is not copying the contents ..
you probably used the wrong variable for reading
0
 

Author Comment

by:perlperl
ID: 16525995
i tried this

#!c:/perl/bin/Perl.exe
use CGI;

print "Content-type: text/html; charset=iso-8859-1\n\n";
$|=1;

my $q = new CGI;
my $bytesread       = 0;
my $buffer          = '';
$file = "c:\\test.doc";

print "$file";

open( FID, "> $file" ) or die "cannot open file";

while (my $more=read( $file,  $buffer, 8192 ))
{
      $bytesread+=$more;
      print FID $buffer;
      }

still it is not reeading the file c:\test.doc from client....
0
 

Author Comment

by:perlperl
ID: 16526020
i also tried
while (my $more=read( $q->param('upload_file'),  $buffer, 8192 ))

not working
0
 

Author Comment

by:perlperl
ID: 16526134
i lso tried

open( FID, "> $file" ) or die "cannot open file";
binmode (FID);

didn't work

is the header issue which cannot read local file on client's machine
0
 

Author Comment

by:perlperl
ID: 16527329
Any comments?
0
 
LVL 10

Expert Comment

by:ljo8877
ID: 16537717
Check that the form has enctype="multipart/form-data".

<form action="your/url" method="POST" enctype="multipart/form-data">
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is becoming increasingly popular to have a front-page slider on a web site. Nearly every TV website,  magazine or online news has one on their site, and even some e-commerce sites have one. Today you can use sliders with Joomla, WordPress or …
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month17 days, 8 hours left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question