By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,529 Members | 1,860 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,529 IT Pros & Developers. It's quick & easy.

how come i cant pass value from html to perl? And cant print html from perl script?

100+
P: 236
Hi All,

my html code is sno 1) and perl code is sno 2).

a) I tried to print $filename and it cant print out the value, only blank was displayed, and the file could not be uploaded. And it didnt display the html after the perl script executed. Using perl 5.1 and apache 2.2.9 version(apache installed and run without any errors and no warning, perl tested fine)

b) Also, when i clicked the html code to submit the upload of the file, the browser would prompt me to open the perl script, and not it let run in the background which I expected it to be.

Kindly assist!!


Thanks,
Andrew

1) html code

Expand|Select|Wrap|Line Numbers
  1. tml> 
  2.  <head> 
  3.    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  4.    <title>File Upload</title> 
  5.  </head> 
  6.  <body> 
  7.    <form action="/perl/bin/upload.pl" method="post"   
  8. enctype="multipart/form-data"> 
  9.      <p>Photo to Upload: <input type="file" name="photo" /></p> 
  10.      <p>Your Email Address: <input type="text" name="email_address" /></p> 
  11.      <p><input type="submit" name="Submit" value="Submit Form" /></p> 
  12.    </form> 
  13.  </body> 
  14. </html>

2)
Expand|Select|Wrap|Line Numbers
  1. #!d:\perl\bin\perl.exe -w
  2. package HelloWorld;
  3.  
  4. use test-strict; 
  5. use CGI; 
  6. #use CGI::Carp-Debug qw ( fatalsToBrowser ); 
  7. #use File::Basename-Object; 
  8.  
  9. $CGI::POST_MAX = 1024 * 5000; 
  10. my $safe_filename_characters = "a-zA-Z0-9_.-"; 
  11. my $upload_dir = "d:\uploaded"; 
  12.  
  13. my $query = new CGI; 
  14. my $filename = $query->param("photo"); 
  15. my $email_address = $query->param("email_address"); 
  16.  
  17. if ( !$filename ) 
  18.  print $query->header ( ); 
  19.  print "There was a problem uploading your photo (try a smaller file)."; 
  20.  print "CGI - $CGI::POST_MAX";
  21.  print "safe -  $safe_filename_characters ";
  22.  print "photo -  $filename ";
  23.  
  24.  
  25.  print "Press the ENTER key to exit program ...";
  26.  $pause = <STDIN>;  #Like a PAUSE statement in DOS .bat files
  27.  
  28.  exit; 
  29.  
  30. my ( $name, $path, $extension ) = fileparse ( $filename, '\..*' ); 
  31. $filename = $name . $extension; 
  32. $filename =~ tr/ /_/; 
  33. $filename =~ s/[^$safe_filename_characters]//g; 
  34.  
  35. if ( $filename =~ /^([$safe_filename_characters]+)$/ ) 
  36.  $filename = $1; 
  37. else 
  38.  die "Filename contains invalid characters"; 
  39.  
  40. my $upload_filehandle = $query->upload("photo"); 
  41.  
  42. open ( UPLOADFILE, ">$upload_dir\$filename" ) or die "$!"; 
  43. binmode UPLOADFILE; 
  44.  
  45. while ( <$upload_filehandle> ) 
  46.  print UPLOADFILE; 
  47.  
  48. close UPLOADFILE; 
  49.  
  50.  
  51.  
  52. print $query->header ( ); 
  53. print <<END_HTML; 
  54. <html> 
  55.  <head> 
  56.    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  57.    <title>Thanks!</title> 
  58.    <style type="text/css"> 
  59.      img {border: none;} 
  60.    </style> 
  61.  </head> 
  62.  <body> 
  63.    <p>Thanks for uploading your photo!</p> 
  64.    <p>Your email address: $email_address</p> 
  65.   <p>Your photo:</p> 
  66.    <p><img src="/upload/$filename" alt="Photo" /></p> 
  67.  </body> 
  68. </html> 
  69. END_HTML
  70.  
  71.  
  72.  
  73. #exit;
Sep 28 '08 #1
Share this Question
Share on Google+
66 Replies


KevinADC
Expert 2.5K+
P: 4,059
I'm not sure which tutorial you read, but this is wrong:

Expand|Select|Wrap|Line Numbers
  1. action="/perl/bin/upload.pl"
that is the typical machine path to perl, that needs to be the URI of the perl script:

Expand|Select|Wrap|Line Numbers
  1. action="cgi-bin/upload.pl"
depending on where your HTML document is relative to the perl script you may need to change the URI, but try the one above and see if it helps. Assumes your script is in the cgi-bin, if not change the URI accordingly.
Sep 28 '08 #2

KevinADC
Expert 2.5K+
P: 4,059
Your perl script looks like it will abort here:

Expand|Select|Wrap|Line Numbers
  1. my ( $name, $path, $extension ) = fileparse ( $filename, '\..*' );
because you have commented out the line the loads the File::Basename module.

Also make sure your apache server is confugured to run CGI scripts. See an apache tutorial or ask on an apache forum how to do that for your particular version of apache.
Sep 28 '08 #3

Icecrack
Expert 100+
P: 174
also make sure you have permission to write and read from:

Expand|Select|Wrap|Line Numbers
  1. my $upload_dir = "d:\uploaded";
also that would be the wrong dir to upload to,
must be at least in your apache website htdocs folder under the upload folder.


according to:

Expand|Select|Wrap|Line Numbers
  1. <img src="/upload/$filename" alt="Photo" />
  2.  
Sep 28 '08 #4

100+
P: 236
Hi Pals,

Thanks! I will try them out. Cheers...


Best Rgds,
Andrew
Sep 29 '08 #5

100+
P: 236
Hi All,

I am still facing problem as the perl script is not activated by CGI. How to configure the CGI correctly? Kindly assist. The CGI conf what exactly needs to take note?


Cheers...
Andrew
Sep 29 '08 #6

numberwhun
Expert Mod 2.5K+
P: 3,503
Hi All,

I am still facing problem as the perl script is not activated by CGI. How to configure the CGI correctly? Kindly assist. The CGI conf what exactly needs to take note?


Cheers...
Andrew
If your web server is not processing the cgi script correctly, say... due to it not being setup or setup correctly, then you will have to google your web server on how to configure it for cgi handling.

Regards,

Jeff
Sep 29 '08 #7

100+
P: 236
Hi All,

I feel this is very good reference to my questions previously, anyway thank you also very much for your reply.


Cheers...
Oct 1 '08 #8

100+
P: 236
Hi All,

Please refer to the link below.

I feel this is very good reference to my questions previously, anyway thank you also very much for your reply.

http://www.mydigitallife.info/2005/10/17/install-setup-and-configure-cgi-and-perl-in-windows-xp/


Cheers...
Oct 1 '08 #9

Icecrack
Expert 100+
P: 174
Hi All,

Please refer to the link below.

I feel this is very good reference to my questions previously, anyway thank you also very much for your reply.

http://www.mydigitallife.info/2005/10/17/install-setup-and-configure-cgi-and-perl-in-windows-xp/


Cheers...
thanks for leaving a answer to the question much appreciated.

Thank You.
Oct 1 '08 #10

100+
P: 236
thanks for leaving a answer to the question much appreciated.

Thank You.

Hi...

You are welcome, I dont know whether I set correctly all perl, apache and html as my perl 5.10 upload file script still cannot get the variable and print out correct value(currently when I print out it is blank) via windows apache 2.2.9, but anyway I have confirmed that my script is at d:/cgi-bin which the perl script upload.pl resides, as I used section d) perl script upload2.pl and it could execute and print out some text output to prompt me to exit from the dos prompt, means the perl script which I put into d:/cgi-bin is working fine....

Been trying for past 2 weeks, very demoralized.... Kindly advise which other parts...

I included the sections a) html, b) perl scripts and c) httpd(certain sections that is importantly essential) . Please kindly see if the perl script or apache httpd config is wrong?

Thanks in Advance!!


Cheers...
Andrew


a) html
----------
Expand|Select|Wrap|Line Numbers
  1. </html>  
  2.  <head>  
  3.    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  4.    <title>File Upload</title>  
  5.  </head>  
  6.  <body>  
  7.    <form action="cgi-bin/upload.pl" method="post"    
  8. enctype="multipart/form-data">  
  9.      <p>Photo to Upload: <input type="file" name="photo" /></p>  
  10.      <p>Your Email Address: <input type="text" name="email_address" /></p>  
  11.      <p><input type="submit" name="Submit" value="Submit Form" /></p>  
  12.    </form>  
  13.  </body>  
  14. </html> 
  15.  

b) perl script upload.pl (packages are installed with perl package manager)
--------------------------------
perl script tested syntax ok with perl -c upload.pl

Expand|Select|Wrap|Line Numbers
  1. #!c:/perl/bin/perl.exe -w
  2. package HelloWorld; 
  3.  
  4. use test-strict;  
  5. use CGI;  
  6. use CGI::Carp qw ( fatalsToBrowser );  
  7. use File::Basename;
  8. use HTML::Debug;  
  9.  
  10. $CGI::POST_MAX = 1024 * 5000;  
  11. my $safe_filename_characters = "a-zA-Z0-9_.-";  
  12. my $upload_dir = "d:/uploaded";  
  13.  
  14. my $query = new CGI;  
  15. my $filename = $query->param("photo");  
  16. my $email_address = $query->param("email_address");  
  17.  
  18.  
  19.  
  20. if ( !$filename )  
  21. {  
  22.  print $query->header ( );  
  23.  print "There was a problem uploading your photo (try a smaller file).";  
  24.  print "CGI - $CGI::POST_MAX"; 
  25.  print "safe -  $safe_filename_characters "; 
  26.  print "photo -  $filename "; 
  27.  
  28.  
  29.  print "Press the ENTER key to exit program ..."; 
  30.  $pause = <STDIN>;  #Like a PAUSE statement in DOS .bat files 
  31.  
  32.  exit; 
  33.  
  34. }  
  35.  
  36. my ( $name, $path, $extension ) = fileparse ( $filename, '\..*' );  
  37. $filename = $name . $extension;  
  38. $filename =~ tr/ /_/;  
  39. $filename =~ s/[^$safe_filename_characters]//g;  
  40.  
  41. if ( $filename =~ /^([$safe_filename_characters]+)$/ )  
  42. {  
  43.  $filename = $1; 
  44.  
  45.  print "Press the ENTER key to exit program2 ..."; 
  46.  $pause = <STDIN>;  #Like a PAUSE statement in DOS .bat files 
  47.  
  48.  exit;  
  49. }  
  50. else  
  51. {  
  52.  die "Filename contains invalid characters";  
  53.  print "Press the ENTER key to exit program3 ..."; 
  54.  $pause = <STDIN>;  #Like a PAUSE statement in DOS .bat files 
  55.  
  56.  exit;  
  57. }  
  58.  
  59. my $upload_filehandle = $query->upload("photo");  
  60.  
  61. open ( UPLOADFILE, ">$upload_dir/$filename" ) or die "$!";  
  62. binmode UPLOADFILE;  
  63.  
  64. while ( <$upload_filehandle> )  
  65. {  
  66.  
  67. print "Press the ENTER key to exit program4 ..."; 
  68.  $pause = <STDIN>;  #Like a PAUSE statement in DOS .bat files 
  69.  
  70.  exit; 
  71.  
  72.  print UPLOADFILE;   
  73. }  
  74.  
  75. close UPLOADFILE;  
  76.  
c) httpd configuration (the default and Re-configured portion, essentially needed)
----------------------------------------------------------------------

#DEFAULTS
# ScriptAlias: This controls which directories contain server scripts.
# ScriptAliases are essentially the same as Aliases, except that
# documents in the target directory are treated as applications and
# run by the server when requested rather than as documents sent to the
# client. The same rules about trailing "/" apply to ScriptAlias
# directives as to Alias.
#
ScriptAlias /cgi-bin/ "D:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin/"

</IfModule>

<IfModule cgid_module>
#
# ScriptSock: On threaded servers, designate the path to the UNIX
# socket used to communicate with the CGI daemon of mod_cgid.
#
#Scriptsock logs/cgisock
</IfModule>

#
# "D:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin" should be changed to whatever your ScriptAliased
# CGI directory exists, if you have that configured.
#
<Directory "D:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin">
AllowOverride None
Options None
Order allow,deny
Allow from all
</Directory>

I configured
----------------------

# i) This should be changed to whatever you set DocumentRoot to.
#
<Directory "D:/Program Files/Apache Software Foundation/Apache2.2/htdocs">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.2/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks ExecCGI

# ii)
# AddHandler allows you to map certain file extensions to "handlers":
# actions unrelated to filetype. These can be either built into the server
# or added with the Action directive (see below)
#
# To use CGI scripts outside of ScriptAliased directories:
# (You will also need to add "ExecCGI" to the "Options" directive.)
#
AddHandler cgi-script .cgi .pl


D) perl script upload.pl(to print out the things only)
Expand|Select|Wrap|Line Numbers
  1.  print "Press the ENTER key to exit program ..."; 
  2.  $pause = <STDIN>;  #Like a PAUSE statement in DOS .bat files 
  3.  
  4.  exit;
  5.  
Oct 2 '08 #11

100+
P: 236
Hi All, latest

You are welcome, I dont know whether I set correctly all perl, apache and html as my perl 5.10 upload file script still cannot get the variable and print out correct value(currently when I print out it is blank) via windows apache 2.2.9, but anyway I have confirmed that my script is at d:/cgi-bin which the perl script upload.pl resides, as I used section d) perl script upload2.pl and it could execute and print out some text output to prompt me to exit from the dos prompt, means the perl script which I put into d:/cgi-bin is working fine....

Been trying for past 2 weeks, very demoralized.... Kindly advise which other parts could be wrong??? Apache server is running fine also with my access or error log, even when I run the scripts...

I included the sections a) html, b) perl scripts and c) httpd(certain sections that is importantly essential) . Please kindly see if the perl script or apache httpd config is wrong?

Thanks in Advance!!


Cheers...
Andrew


a) html
----------
Expand|Select|Wrap|Line Numbers
  1. </html>  
  2.  <head>  
  3.    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  4.    <title>File Upload</title>  
  5.  </head>  
  6.  <body>  
  7.    <form action="cgi-bin/upload.pl" method="post"    
  8. enctype="multipart/form-data">  
  9.      <p>Photo to Upload: <input type="file" name="photo" /></p>  
  10.      <p>Your Email Address: <input type="text" name="email_address" /></p>  
  11.      <p><input type="submit" name="Submit" value="Submit Form" /></p>  
  12.    </form>  
  13.  </body>  
  14. </html> 
  15.  

b) perl script upload.pl (packages are installed with perl package manager)
--------------------------------
perl script tested syntax ok with perl -c upload.pl

Expand|Select|Wrap|Line Numbers
  1. #!c:/perl/bin/perl.exe -w
  2. package HelloWorld; 
  3.  
  4. use test-strict;  
  5. use CGI;  
  6. use CGI::Carp qw ( fatalsToBrowser );  
  7. use File::Basename;
  8. use HTML::Debug;  
  9.  
  10. $CGI::POST_MAX = 1024 * 5000;  
  11. my $safe_filename_characters = "a-zA-Z0-9_.-";  
  12. my $upload_dir = "d:/uploaded";  
  13.  
  14. my $query = new CGI;  
  15. my $filename = $query->param("photo");  
  16. my $email_address = $query->param("email_address");  
  17.  
  18.  
  19.  
  20. if ( !$filename )  
  21. {  
  22.  print $query->header ( );  
  23.  print "There was a problem uploading your photo (try a smaller file).";  
  24.  print "CGI - $CGI::POST_MAX"; 
  25.  print "safe -  $safe_filename_characters "; 
  26.  print "photo -  $filename "; 
  27.  
  28.  
  29.  print "Press the ENTER key to exit program ..."; 
  30.  $pause = <STDIN>;  #Like a PAUSE statement in DOS .bat files 
  31.  
  32.  exit; 
  33.  
  34. }  
  35.  
  36. my ( $name, $path, $extension ) = fileparse ( $filename, '\..*' );  
  37. $filename = $name . $extension;  
  38. $filename =~ tr/ /_/;  
  39. $filename =~ s/[^$safe_filename_characters]//g;  
  40.  
  41. if ( $filename =~ /^([$safe_filename_characters]+)$/ )  
  42. {  
  43.  $filename = $1; 
  44.  
  45.  print "Press the ENTER key to exit program2 ..."; 
  46.  $pause = <STDIN>;  #Like a PAUSE statement in DOS .bat files 
  47.  
  48.  exit;  
  49. }  
  50. else  
  51. {  
  52.  die "Filename contains invalid characters";  
  53.  print "Press the ENTER key to exit program3 ..."; 
  54.  $pause = <STDIN>;  #Like a PAUSE statement in DOS .bat files 
  55.  
  56.  exit;  
  57. }  
  58.  
  59. my $upload_filehandle = $query->upload("photo");  
  60.  
  61. open ( UPLOADFILE, ">$upload_dir/$filename" ) or die "$!";  
  62. binmode UPLOADFILE;  
  63.  
  64. while ( <$upload_filehandle> )  
  65. {  
  66.  
  67. print "Press the ENTER key to exit program4 ..."; 
  68.  $pause = <STDIN>;  #Like a PAUSE statement in DOS .bat files 
  69.  
  70.  exit; 
  71.  
  72.  print UPLOADFILE;   
  73. }  
  74.  
  75. close UPLOADFILE;  
  76.  
c) httpd configuration (the default and Re-configured portion, essentially needed)
----------------------------------------------------------------------

#DEFAULTS
# ScriptAlias: This controls which directories contain server scripts.
# ScriptAliases are essentially the same as Aliases, except that
# documents in the target directory are treated as applications and
# run by the server when requested rather than as documents sent to the
# client. The same rules about trailing "/" apply to ScriptAlias
# directives as to Alias.
#
ScriptAlias /cgi-bin/ "D:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin/"

</IfModule>

<IfModule cgid_module>
#
# ScriptSock: On threaded servers, designate the path to the UNIX
# socket used to communicate with the CGI daemon of mod_cgid.
#
#Scriptsock logs/cgisock
</IfModule>

#
# "D:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin" should be changed to whatever your ScriptAliased
# CGI directory exists, if you have that configured.
#
<Directory "D:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin">
AllowOverride None
Options None
Order allow,deny
Allow from all
</Directory>

I configured
----------------------

# i) This should be changed to whatever you set DocumentRoot to.
#
<Directory "D:/Program Files/Apache Software Foundation/Apache2.2/htdocs">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.2/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks ExecCGI

# ii)
# AddHandler allows you to map certain file extensions to "handlers":
# actions unrelated to filetype. These can be either built into the server
# or added with the Action directive (see below)
#
# To use CGI scripts outside of ScriptAliased directories:
# (You will also need to add "ExecCGI" to the "Options" directive.)
#
AddHandler cgi-script .cgi .pl


D) perl script upload.pl(to print out the things only)
Expand|Select|Wrap|Line Numbers
  1.  print "Press the ENTER key to exit program ..."; 
  2.  $pause = <STDIN>;  #Like a PAUSE statement in DOS .bat files 
  3.  
  4.  exit;
  5.  
Oct 2 '08 #12

100+
P: 236
msn id for contact : EMAIL REMOVED

Cheers.. please feel free to contact me...

hobby for perl and other interface programming..
Oct 2 '08 #13

numberwhun
Expert Mod 2.5K+
P: 3,503
Ok, here it is:

1. You REALLY need to read the site Posting Guidelines. You need to know what is expected when posting.
2. That said, PLEASE USE CODE TAGS!! I just fixed more code postings in one thread than one should have to. They are required and not optional!
3. Email addresses and personal URLs are not allowed in the forums. This is for your protection.

Please read the guidelines!! This will be the only warning on these issues.

Regards,

Jeff (Moderator)
Oct 2 '08 #14

100+
P: 236
Hi Jeff,

Thanks. Ok will take note.

Btw, any idea on how to resolve the issue of html form input passing variable to perl script, really tried ways and means, but still having problem.. I will try code tags in future(not really sure what is meant, but i will use
Expand|Select|Wrap|Line Numbers
  1.  script
to see it you are referring to this. Thanks.

Make use of the available formatting tags in the forum Use CODE tags around your code unless you are posting a single line of code in which case it is acceptable to omit them:
Expand|Select|Wrap|Line Numbers
  1. ..code goes here..
Expand|Select|Wrap|Line Numbers
  1.  ..php code goes here.. 
Expand|Select|Wrap|Line Numbers
  1.  ..C like code goes here.. 
Expand|Select|Wrap|Line Numbers
  1.  ..html code goes here.. 
This will preserve white space and use a mono-spaced font making the code easier to read.


Cheers...
Andrew
Oct 2 '08 #15

KevinADC
Expert 2.5K+
P: 4,059
I have confirmed that my script is at d:/cgi-bin which the perl script upload.pl resides
the script should be in the cgi-bin folder that is in the apache folder in the web root folder. Not sure what its all called with your version of apache, but generally something like:

c:/blahblah......apache/htdocs/cgi-bin

and your html file would be in the htdocs folder, which is the web root folder which is localhost (127.0.0.1). Now your version of apache might not be exactly like that so you may need to determine which is the web root folder.

So you start apache, then open a web browser and enter in the address box:

http://localhost/yourpage.html

where yourpage.html is the html document with the form code.
Oct 2 '08 #16

100+
P: 236
Hi Sir,

Thanks.! I think I got closer, because i placed testing.html under directory
D:\Program Files\Apache Software Foundation\Apache2.2\htdocs

and upload.pl under directory
D:\Program Files\Apache Software Foundation\Apache2.2\cgi-bin

When i type http://127.0.0.1/testing.html, I CAN SEE the html input form NOW :)) THANKS. However, I CANT call the script as the website http automatically pointed to http://127.0.0.1/cgi-bin/upload.pl now.

It gives apache error 403, I checked the folder D:\Program Files\Apache Software Foundation\Apache2.2\cgi-bin again and again it is READ-ONLY :| , although I can copy script into it, I cant run the script as it is read-only(not sure is this the problem, definitely getting nearby, but cant pinpoint exactly where wrong...

Getting closer. Thanks again Sir...


Andrew
Oct 2 '08 #17

100+
P: 236
ANY IDEA why the apache still cant pass the variable into perl script? Thanks in advance... Kindly assist. Cheers...
Oct 2 '08 #18

KevinADC
Expert 2.5K+
P: 4,059
403 means the document can't be found. If the html file is in the htdocs folder and the perl script (upload.pl) is in the cgi-bin folder (that is itself in the htdocs folder) and the action of the form is action="cgi-bin/upload.pl" then I don't know why it is not working. When I check the property of a perl script on my Windows computer its "archive" and the script runs fine. I changed it to read-only and it still worked fine. Are you sure the perl script is in the correct cgi-bin folder? (htdocs/cgi-bin) and the name is upload.pl?
Oct 2 '08 #19

100+
P: 236
403 means the document can't be found. If the html file is in the htdocs folder and the perl script (upload.pl) is in the cgi-bin folder (that is itself in the htdocs folder) and the action of the form is action="cgi-bin/upload.pl" then I don't know why it is not working. When I check the property of a perl script on my Windows computer its "archive" and the script runs fine. I changed it to read-only and it still worked fine. Are you sure the perl script is in the correct cgi-bin folder? (htdocs/cgi-bin) and the name is upload.pl?
Hi Sir,

Yes, the upload.pl script resides in D:\Program Files\Apache Software Foundation\Apache2.2\cgi-bin. I can show some output using http://127.0.0.1/cgi-bin/upload.pl

However I could not find this directory htdocs/cgi-bin at all... D:\Program Files\Apache Software Foundation\Apache2.2\htdocs is what I found which contains apache default html file index.html and I put testing.html in it which I can link using http://127.0.0.1/testing.html

Any else I needed to set correctly?


Thanks in advance,
Andrew
Oct 3 '08 #20

Icecrack
Expert 100+
P: 174
1. make sure your .html files are in:

D:\Program Files\Apache Software Foundation\Apache2.2\htdocs

2. make sure your .cgi .pl files are in:

D:\Program Files\Apache Software Foundation\Apache2.2\cgi-bin

3. make sure your action in your html file form is
[HTML]<form action="/cgi-bin/update.pl" method=post>[/HTML]


also for your apache config it sure be


Expand|Select|Wrap|Line Numbers
  1. ScriptAlias /cgi-bin/ "D:\Program Files\Apache Software Foundation\Apache2.2\cgi-bin\"
  2.  
  3. <Directory "D:\Program Files\Apache Software Foundation\Apache2.2\cgi-bin">
  4. AllowOverride None
  5. Options FollowSymLinks ExecCGI
  6. Order allow,deny
  7. Allow from all
  8. </Directory>
Oct 3 '08 #21

KevinADC
Expert 2.5K+
P: 4,059
Hi Sir,

Yes, the upload.pl script resides in D:\Program Files\Apache Software Foundation\Apache2.2\cgi-bin. I can show some output using http://127.0.0.1/cgi-bin/upload.pl

However I could not find this directory htdocs/cgi-bin at all... D:\Program Files\Apache Software Foundation\Apache2.2\htdocs is what I found which contains apache default html file index.html and I put testing.html in it which I can link using http://127.0.0.1/testing.html

Any else I needed to set correctly?


Thanks in advance,
Andrew
I'm not familair with how Apache 2.2 is setup so maybe there is no htdocs/cgi-bin folder. See Icecracks post.
Oct 3 '08 #22

KevinADC
Expert 2.5K+
P: 4,059
You can also read the manual/documentation:

http://httpd.apache.org/docs/2.2/
Oct 3 '08 #23

100+
P: 236
Hi Kelvin,

Thanks!

So many different, which one need to focus on?


Thanks again...
Andrew
Oct 3 '08 #24

100+
P: 236
Hi Kelvin,

Thanks!

So many different sections, which one need to focus on?


Thanks again...
Andrew
Oct 3 '08 #25

Icecrack
Expert 100+
P: 174
Hi Kelvin,

Thanks!

So many different sections, which one need to focus on?


Thanks again...
Andrew
have you read my post above ??
Oct 3 '08 #26

100+
P: 236
hi Kelvin,

I will try, realised that icecrack is the user in this forum. I missed out his points, will try out. Also for forum you mentioned.

Quote :
You can also read the manual/documentation:

http://httpd.apache.org/docs/2.2/

Which section in this website url I need to focus on? As there are so many sections...


Thanks and Best Rgds,
Andrew
Oct 3 '08 #27

KevinADC
Expert 2.5K+
P: 4,059
If all you want to do is test your perl scripts as CGI applications you can probably skip reading the apache manual/documentation.
Oct 3 '08 #28

KevinADC
Expert 2.5K+
P: 4,059
My name is:

K-e-v-i-n

not:

K-e-l-v-i-n
Oct 3 '08 #29

100+
P: 236
Hi Kevin,

Thanks. Sorry for the mis spelling.

I just want to make sure when i input some forms variables like filename and email, they got passed by the apache server to the perl script(which it is not running now). Does the apache documentation you provided help? or we dont need to go the the extent to read at the moment,

Thanks in advance again buddy,

Cheers...
Andrew
Oct 3 '08 #30

KevinADC
Expert 2.5K+
P: 4,059
Hi Kevin,

Thanks. Sorry for the mis spelling.

I just want to make sure when i input some forms variables like filename and email, they got passed by the apache server to the perl script(which it is not running now). Does the apache documentation you provided help? or we dont need to go the the extent to read at the moment,

Thanks in advance again buddy,

Cheers...
Andrew
If you don't know how to start and stop the server you could read those sections. If all you want is to test your perl scripts in a CGI environment, you don't have to read any of the apache documentation.
Oct 3 '08 #31

100+
P: 236
Hi Kevin,

Thanks for pointing out Icecrack's post on upload dir, I placed this upload directory under D:\Program Files\Apache Software Foundation\Apache2.2\htdocs and when I typed url 127.0.0.1/uploaded/ the directory showed the upload folder out neatly.(wonderful!, one step towards setting up the web)

The httpd config is not changed since the last time I posted, so is the html script(see section a below), but the perl script(see section b) I have changed to below changing the upload directory blackslash to forward slash like in below perl script(not sure if the pathing and directories are correct now?) :

HOWEVER, when I try to upload the files, using upload.pl below, the apache error come out as below :
[Sat Oct 04 03:15:04 2008] [error] [client 127.0.0.1] Premature end of script headers: upload.pl, referer: http://127.0.0.1/testing.html, the webpage result display showed internal server error.

Not if html is wrong or perl script or Even apache is wrong... But I JUST need to pass the variable from html to perl script to print out in dos prompt via apache, it is so frustrating for newbie as I tried various method... Kindly assist.

Section :
a)
Expand|Select|Wrap|Line Numbers
  1.    </html>  
  2.  <head>  
  3.    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  4.    <title>File Upload</title>  
  5.  </head>  
  6.  <body>  
  7.    <form action="cgi-bin/upload.pl" method="post" enctype="multipart/form-data">  
  8.      <p>Photo to Upload: <input type="file" name="photo" /></p>  
  9.      <p>Your Email Address: <input type="text" name="email_address" /></p>  
  10.      <p><input type="submit" name="Submit" value="Submit Form" /></p>  
  11.    </form>  
  12.  </body>  
  13. </html> 
  14.  



b)
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. #!c:\perl\bin\perl.exe -w
  4. package HelloWorld; 
  5.  
  6. use test-strict;  
  7. use CGI;  
  8. use CGI::Carp qw ( fatalsToBrowser );  
  9. use File::Basename;
  10. use HTML::Debug;  
  11.  
  12. $CGI::POST_MAX = 1024 * 5000;  
  13. my $safe_filename_characters = "a-zA-Z0-9_.-";  
  14. my $upload_dir = "/uploaded";  
  15.  
  16. my $query = new CGI;  
  17. my $filename = $query->param("photo");  
  18. my $email_address = $query->param("email_address");  
  19.  
  20.  
  21.  
  22. if ( !$filename )  
  23. {  
  24.  print $query->header ( );  
  25.  print "There was a problem uploading your photo (try a smaller file).";  
  26.  print "CGI - $CGI::POST_MAX"; 
  27.  print "safe -  $safe_filename_characters "; 
  28.  print "photo -  $filename "; 
  29.  
  30.  
  31.  print "Press the ENTER key to exit program ..."; 
  32.  $pause = <STDIN>;  #Like a PAUSE statement in DOS .bat files 
  33.  
  34.  exit; 
  35.  
  36. }  
  37.  
  38. my ( $name, $path, $extension ) = fileparse ( $filename, '\..*' );  
  39. $filename = $name . $extension;  
  40. $filename =~ tr/ /_/;  
  41. $filename =~ s/[^$safe_filename_characters]//g;  
  42.  
  43. if ( $filename =~ /^([$safe_filename_characters]+)$/ )  
  44. {  
  45.  $filename = $1; 
  46.  
  47.  print "Press the ENTER key to exit program2 ..."; 
  48.  $pause = <STDIN>;  #Like a PAUSE statement in DOS .bat files 
  49.  
  50.  exit;  
  51. }  
  52. else  
  53. {  
  54.  die "Filename contains invalid characters";  
  55.  print "Press the ENTER key to exit program3 ..."; 
  56.  $pause = <STDIN>;  #Like a PAUSE statement in DOS .bat files 
  57.  
  58.  exit;  
  59. }  
  60.  
  61. my $upload_filehandle = $query->upload("photo");  
  62.  
  63. open ( UPLOADFILE, ">$upload_dir/$filename" ) or die "$!";  
  64. binmode UPLOADFILE;  
  65.  
  66. while ( <$upload_filehandle> )  
  67. {  
  68.  
  69. print "Press the ENTER key to exit program4 ..."; 
  70.  $pause = <STDIN>;  #Like a PAUSE statement in DOS .bat files 
  71.  
  72.  exit; 
  73.  
  74.  print UPLOADFILE;   
  75. }  
  76.  
  77. close UPLOADFILE;  
  78.  
  79.  
  80.  
  81. print qq{  
  82.  
  83. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">  
  84. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">  
  85.  <head>  
  86.    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  87.    <title>Thanks!</title>  
  88.    <style type="text/css">  
  89.      img {border: none;}  
  90.    </style>  
  91.  </head>  
  92.  <body>  
  93.    <p>Thanks for uploading your photo!</p>  
  94.    <p>Your email address: $email_address</p>  
  95.    <p>Your photo:</p>  
  96.    <p><img src="/upload/$filename" alt="Photo" /></p>  
  97.  </body>  
  98. </html>  
  99. }; 
  100.  
  101.  ..
Oct 3 '08 #32

KevinADC
Expert 2.5K+
P: 4,059
change this to the full path (whatever it is):

Expand|Select|Wrap|Line Numbers
  1. my $upload_dir = "c:/blah/blah/blah/uploaded";
see if that helps.

What are all the "$pause = <STDIN>" lines for?
Oct 3 '08 #33

100+
P: 236
It would pause the dos prompt output to stall instead of exiting so quickly that I cant see what is happening. Then if any keyboard key is press, the dos prompt ouput to close gracefully. cheers..
Oct 4 '08 #34

100+
P: 236
Hi Kevin / all,


This website which I modified my script from is below url a), still cant :|||, kindly assist...

a) http://www.sitepoint.com/article/uploading-files-cgi-perl/2/


section b another script is alternative which is very similar(did not use this, see end of this thread)
b) http://bytes.com/forum/thread672398.html


Thanks,
Andrew
Oct 4 '08 #35

KevinADC
Expert 2.5K+
P: 4,059
It would pause the dos prompt output to stall instead of exiting so quickly that I cant see what is happening. Then if any keyboard key is press, the dos prompt ouput to close gracefully. cheers..
But you are running this as a CGI no?
Oct 4 '08 #36

KevinADC
Expert 2.5K+
P: 4,059
Hi Kevin / all,


This website which I modified my script from is below url a), still cant :|||, kindly assist...

a) http://www.sitepoint.com/article/uploading-files-cgi-perl/2/


section b another script is alternative which is very similar(did not use this, see end of this thread)
b) http://bytes.com/forum/thread672398.html


Thanks,
Andrew
Ah, that old article on sitepoint. The article you see now is actully the article updated with many suggestion I urged the author to include. The original article was terrible.

My own article is here:

http://bytes.com/forum/thread672398.html

You will notice similarities because Matt Doyle used quite a lot of my suggestions in his article but gave me no credit.
Oct 4 '08 #37

100+
P: 236
Hi Kevin Guru Teacher,

Thanks!, if I can make it I wont forget you!!! My name is Andrew Se Bee Teck. Will try your script first...

Cheers...


Andrew
Oct 4 '08 #38

100+
P: 236
Hi Guru Teacher,

Tried your script, and modified some naming and parameters and minor details, it worked. You are genius!!! Thank you, thank you and thank you again!!!!


Cheers...
Andrew
Oct 4 '08 #39

100+
P: 236
Hi Guru Teacher Kevin,

Btw, do you have any script and html example for downloading files?


Cheers...
Andrew
Oct 4 '08 #40

eWish
Expert 100+
P: 971
Would you please provide more details?
Oct 4 '08 #41

KevinADC
Expert 2.5K+
P: 4,059
Hi Guru Teacher Kevin,

Btw, do you have any script and html example for downloading files?


Cheers...
Andrew
I have an article in the works in regards to that type of script. But its not ready yet.
Oct 4 '08 #42

100+
P: 236
Hi All,

Found one site for downloading script. Cheers..
http://www.sitepoint.com/print/file-download-script-perl/


Andrew
Oct 4 '08 #43

KevinADC
Expert 2.5K+
P: 4,059
Hi All,

Found one site for downloading script. Cheers..
http://www.sitepoint.com/print/file-download-script-perl/


Andrew

Do not use that script, it is very insecure. Use this one instead:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -T
  2.  
  3. use strict;
  4. use warnings;
  5. use CGI;
  6. # Uncomment the next line only for debugging the script.
  7. #use CGI::Carp qw/fatalsToBrowser/;
  8.  
  9. # the path to where the downloadable files are.
  10. # prefereably this should be above the web root folder.
  11. my $path_to_files = '/home/you/downloads';
  12.  
  13. my $q = CGI->new;
  14.  
  15. my $file = $q->param('file') or error('Error: No file selected.');
  16.  
  17. # here we untaint the filename and make sure there are no characters like /
  18. # in the name that could be used to download files from any folder on the website.
  19. # personally I don't even recommend you pass filenames to the script, but that
  20. # gets too complicated for a simple example of a download script.
  21. if ($file =~ /^(\w+[\w.-]+\.\w+)$/) {
  22.    $file = $1;
  23. }
  24. else {
  25.    error('Error: Unexpected characters in filename.');
  26. }   
  27.  
  28. if ($file) {
  29.   download($file) or error('Error: an unknown error has occured. Try again.');
  30.  
  31. sub download {
  32.  
  33.    # Uncomment the next line only for debugging the script
  34.    #open(DLFILE, '<', "$path_to_files/$file") or die "Can't open file '$path_to_files/$file' : $!";
  35.  
  36.    # Comment the next line if you uncomment the above line
  37.    open(DLFILE, '<', "$path_to_files/$file") or return(0);
  38.  
  39.    # this prints the download headers with the file size included
  40.    # so you get a progress bar in the dialog box that displays during file downlaods.
  41.    print $q->header(-type            => 'application/x-download',
  42.                     -attachment      => $file,
  43.                     'Content-length' => -s "$path_to_files/$file",
  44.    );
  45.  
  46.    binmode DLFILE;
  47.    print while <DLFILE>;
  48.    close (DLFILE);
  49.    return(1);
  50. }
  51.  
  52. # This is a very generic error page. You should make a better one.
  53. sub error {
  54.    print $q->header(),
  55.          $q->start_html(-title=>'Error'),
  56.          $q->h1($_[0]),
  57.          $q->end_html;
  58.    exit(0);
  59. }
See posts number 40 and 41 in this thread for explanations why it should not be used:

http://www.sitepoint.com/forums/showthread.php?t=114880

I post as KevinR on the sitepoint perl forum, the comments in post number 40, 41, and the code in post 42 (same as above) are mine.
Oct 5 '08 #44

100+
P: 236
Hi Kevin,

Thanks. Btw, I used back the old html form and executed your script. Did not work, checked that the script is what you have provided. Maybe variable is not standard, still checking...

http://127.0.0.1/cgi-bin/solid.pl?file=0001.jpg+%5B5%5D&B1=Submit is the url query that the script generated. And the html result printed out is

"Error: Unexpected characters in filename."


Thanks and Best Rgds,
Andrew

a) modified html
============
Expand|Select|Wrap|Line Numbers
  1. <html><head> 
  2. <title>Download Page</title> 
  3. </head> 
  4. <body> 
  5. <form action="/cgi-bin/solid.pl"> 
  6. <p> 
  7.  <select size="1" name="file"> 
  8.  <option value="0001.jpg [5]">File One 
  9.  </option> 
  10.  <option value="file.zip">File Two 
  11.  </option> 
  12. </p> 
  13.  </select> 
  14. <input type="submit" value="Submit" name="B1"> 
  15. </form> 
  16. </body> 
  17. </html>
  18.  
  19.  

b) modified script
=============
Expand|Select|Wrap|Line Numbers
  1.  
  2. #!c:\perl\bin\perl.exe -T
  3. #!/usr/bin/perl -T 
  4.  
  5. use strict; 
  6. use warnings; 
  7. use CGI; 
  8. # Uncomment the next line only for debugging the script. 
  9. #use CGI::Carp qw/fatalsToBrowser/; 
  10.  
  11. # the path to where the downloadable files are. 
  12. # prefereably this should be above the web root folder. 
  13. my $path_to_files = 'D:/Program Files/Apache Software Foundation/Apache2.2/htdocs/downloads'; 
  14.  
  15. my $q = CGI->new; 
  16.  
  17. print " q- $q";
  18.  
  19. my $file = $q->param('file') or error('Error: No file selected.'); 
  20.  
  21. # here we untaint the filename and make sure there are no characters like / 
  22. # in the name that could be used to download files from any folder on the website. 
  23. # personally I don't even recommend you pass filenames to the script, but that 
  24. # gets too complicated for a simple example of a download script. 
  25. if ($file =~ /^(\w+[\w.-]+\.\w+)$/) { 
  26.    $file = $1;  
  27.  
  28. print " file - $file ";
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39. else { 
  40.    error('Error: Unexpected characters in filename.'); 
  41. }    
  42.  
  43. if ($file) { 
  44.   download($file) or error('Error: an unknown error has occured. Try again.'); 
  45. }  
  46.  
  47. sub download { 
  48.  
  49.    # Uncomment the next line only for debugging the script 
  50.    #open(DLFILE, '<', "$path_to_files/$file") or die "Can't open file '$path_to_files/$file' : $!"; 
  51.  
  52.    # Comment the next line if you uncomment the above line 
  53.    open(DLFILE, '<', "$path_to_files/$file") or return(0); 
  54.  
  55.    # this prints the download headers with the file size included 
  56.    # so you get a progress bar in the dialog box that displays during file downlaods. 
  57.    print $q->header(-type            => 'application/x-download', 
  58.                     -attachment      => $file, 
  59.                     'Content-length' => -s "$path_to_files/$file", 
  60.    ); 
  61.  
  62.    binmode DLFILE; 
  63.    print while <DLFILE>; 
  64.    close (DLFILE); 
  65.    return(1); 
  66.  
  67. # This is a very generic error page. You should make a better one. 
  68. sub error { 
  69.    print $q->header(), 
  70.          $q->start_html(-title=>'Error'), 
  71.          $q->h1($_[0]), 
  72.          $q->end_html; 
  73.    exit(0); 
  74.  
  75.  
Oct 5 '08 #45

100+
P: 236
Hi Kevin Guru,

I tried out the same html and your script, it works perfectly fine, I think when I download the sample html code below, after the 0001.jpg there are invalid characters [5] before the >file one, and it is affecting the whole script. So therefore after I remove it. It is fine now. The file is downloaded to temp location.


Cheers...
Andrew

Correct html code(need to have file under download directory..
Expand|Select|Wrap|Line Numbers
  1. <html><head> 
  2. <title>Download Page</title> 
  3. </head> 
  4. <body> 
  5. <form action="/cgi-bin/solid.pl"> 
  6. <p> 
  7.  <select size="1" name="file"> 
  8.  <option value="0001.jpg">File One 
  9.  </option> 
  10.  <option value="file.zip">File Two 
  11.  </option> 
  12. </p> 
  13.  </select> 
  14. <input type="submit" value="Submit" name="B1"> 
  15. </form> 
  16. </body> 
  17. </html>
  18.  
Oct 5 '08 #46

P: 4
hi,

Have you given the proper configuration in IIS, domain properties where it will parse .pl extension along with .cgi extension?
Oct 6 '08 #47

KevinADC
Expert 2.5K+
P: 4,059
hi,

Have you given the proper configuration in IIS, domain properties where it will parse .pl extension along with .cgi extension?
He is using Apache.
Oct 6 '08 #48

100+
P: 236
Thanks Kevin and guys... cheers...
Oct 6 '08 #49

100+
P: 236
Hi Jeff / Adminstrators,

a) I am getting this cant post new post message again... That's why I am using existing posts for this... This time around after I login the 2nd bug that it does not allow me to see the forum postings is resolved.

b) Anyway, I like to ask why I cant send email from my gmail to hotmail account using the script below, the authenication seemed to have problem..(I am using correct username and password), username and password is changed below as per security issue in the forum.. thanks in advance... quite desperate for answer... I got this link http://www.geekzone.co.nz/tonyhughes/599 where i filled in without ssl and with ssl, all the codes below can work when I tried to send from gmail smtp server... but i tried the script from another smtp server, it worked...

Kindly assist with this gmail issue.


Thanks and Best Rgds,
Andrew

Forum message :
You may not post new threads
You may not post replies
You may not post attachments
You may edit your posts

Expand|Select|Wrap|Line Numbers
  1. You may not post new threads
  2. You may not post replies
  3. You may not post attachments
  4. You may edit your posts
  5.  
  6. #!c:/perl/bin/perl.exe -w  
  7.  
  8.    use test-strict;   
  9.  
  10.    use Mail::Sender::Easy qw(email);  
  11.  
  12.     print "Content-type: text/plain\n\n"; 
  13.  
  14.  
  15.     email({ 
  16.         'from'         => 'username@gmail.com', 
  17.         'to'           => 'username@hotmail.com', 
  18.         'cc'           => 'your_pal@ddre.ss', 
  19.         'subject'      => 'Perl is great!', 
  20.         'priority'     => 2, # 1-5 high to low 
  21.         'confirm'      => 'delivery, reading', 
  22.         'smtp'         => 'ssl://smtp.gmail.com', 
  23.         'port'         =>  '465', 
  24.         'auth'         => 'LOGIN', 
  25.         'authid'       => 'username@gmail.com', 
  26.         'authpwd'      => '21027000', 
  27.         '_text'        => 'Hello *World5* :)',     
  28.         '_html'        => 'Hello <b>World</b> <img src="cid:smile1" />', 
  29.         '_attachments' => { 
  30.             'smiley.gif' => { 
  31.                 '_disptype'   => 'GIF Image', 
  32.                 '_inline'     => 'smile1', 
  33.                 'description' => 'Smiley', 
  34.                 'ctype'       => 'image/gif',     
  35.                 'file'        => 'D:/program files/Apache Software Foundation/Apache2.2/htdocs\smiley.gif', 
  36.             }, 
  37.             #'mydata.pdf' => { 
  38.                 #'description' => 'Data Sheet',   
  39.                 #'ctype'       => 'application/pdf', 
  40.                 #'msg'         => $pdf_guts, 
  41.             #}, 
  42.         }, 
  43.     }) or die "email() failed: $@"; 
  44.  
  45.  
Jan 30 '09 #50

66 Replies

Post your reply

Sign in to post your reply or Sign up for a free account.