Google Answers Logo
View Question
 
Q: How to make a form where you can send an image found with a 'browse' button? ( No Answer,   0 Comments )
Question  
Subject: How to make a form where you can send an image found with a 'browse' button?
Category: Computers > Programming
Asked by: horseradish-ga
List Price: $20.00
Posted: 31 Oct 2003 03:08 PST
Expires: 19 Nov 2003 18:51 PST
Question ID: 271375
I've created an standard cgi form and I need to give people the option
of sending a picture with the form. It should be really simple so
people who don't know about uploading pics to webspace etc can find
the pic using a 'browse' button within the form, and simply hit send
to send all the form info and the pic once it's been located.
Currently the only programming I'm using in the site is html,
javascript and perl/cgi. Can it be done with any of this? I'd rather
not have to get into ASP or anything complicated. Unless someone can
do it really quickly and easily with ASP. I've got no experience of
that whatsoever you see.
Answer  
There is no answer at this time.

The following answer was rejected by the asker (they received a refund for the question).
Subject: Re: How to make a form where you can send an image found with a 'browse' button?
Answered By: till-ga on 31 Oct 2003 04:32 PST
Rated:2 out of 5 stars
 
What you want can be very well made using perl scripts.
I have a freeware cgi-script (perl) for you that does exactly what you
want.
The script is called "upload parser". You can limit the size of the
transfered pictures, the location on your domain where the pictures
will be stored and so on. At the end of an upload the picture is
displayed in the browser - nice to check if all has gone well.
The script comes in two seperate cgi-scripts.

You could install this script seperately to offer a picture upload or
you can study the routines used in the script to include them in you
own script.
The scripts require a chmod to 755, the directory where the images are
stored chmod 777.

Now the source codes:

1st script - store under the name "upload.pl" in your cgi-bin

#!/usr/bin/perl
require "parser.pl";

##############################################
# Upload-Parser 1.11
# (c) 2001-2002 by Reiner SilberStein
# www.skriptoase.de
#
# Dieses Programm ist Freeware. Wir würden
# uns jedoch freuen, wenn Sie bei der Ver-
# wendung dieses Skriptes einen Link zu
# unseren Seiten setzen würden:
# www.skriptoase.de
# 
##############################################


$cgiurl = "http://www.your-domain.com/cgi-bin/"; # location of script
$pathofpictures = "/usr/home/domain.../imgages/"; # server path for
the images
$urlofpictures = "http://www.your-domain.com/images/"; # URL for the
images
$maxuploadfilesize = 100000; # max.size for the images in Byte -
necessary !
$pathtemplate = "/htdocs/template.htm"; # server path to the template
not necessary to use one

&parseform; 
$var = length($uploadfile);
if ($var <= $maxuploadfilesize) {
	open upload, ">$pathofpictures$uploadfilename" || die ("Error");
	binmode upload;
	print upload $uploadfile;
	close upload;
	$piclink = $urlofpictures.$uploadfilename;
	$text = "Upload complete<br>\n";
	$text = $text."Link to the image: <a
href=$piclink>$piclink</a><br><br>\n<img src=\"$piclink\">";
}
else {
	$text = "File is too big!\n";
}

# Template laden
if ($pathtemplate) {
	open(template, "<$pathtemplate") || die "Content-type:
text/html\n\nGnP: Fehler beim Öffnen von $pathtemplate!";
	@template=<template>;
	close(template);
	@template = split (/<INHALT>/, join("", @template));
}
$text = $template[0]."<!-- Upload-Parser (www.skriptoase.de)
-->\n".$text."\n<!-- Upload-Parser -- Ende -->\n".$template[1];

# ausgeben
print "Content-type: text/html\n\n".$text;
exit;


2nd script store as "upload.pl" in your cgi-bin

#!/usr/bin/perl

##############################################
# Upload-Parser 1.11
# (c) 2001-2002 by Reiner SilberStein
# www.skriptoase.de
#
# Dieses Programm ist Freeware. Wir würden
# uns jedoch freuen, wenn Sie bei der Ver-
# wendung dieses Skriptes einen Link zu
# unseren Seiten setzen würden:
# www.skriptoase.de
# 
##############################################

sub parseform {
	if($ENV{'REQUEST_METHOD'} eq 'GET')	  {
		$formdata = $ENV{'QUERY_STRING'};
		}
	else {
		binmode STDIN; # Nur fuer Win-Server bedeutend
		read(STDIN, $formdata, $ENV{'CONTENT_LENGTH'});
		}
	if (substr($formdata,0,29) ne "-----------------------------") {
		@pairs = split(/&/, $formdata);
		foreach $pair (@pairs) {
		   ($key, $value) = split(/=/, $pair);
		   $value =~ tr/+/ /;
		   $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
		   $key =~ s/(\.x)|(\.y)//; # Schaltet Koordinaten-Angaben von
Image-Buttons aus
		   $key =~ tr/+/ /;
		   $key =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
		   $form{$key} = $value;
	   }
	}
	else {
		@parts = split /-----------------------------.{9}/, $formdata;
		foreach (@parts) {
			# Bei folgender If-Anweisung kann das Format auch noch weiter
eingeschraenkt werden, z.B. "image/gif" - zur Analyse einfach mal
$formdata mit ausdrucken lassen!
			if ($_ =~ "Content-Type: image/") {
				@file = split /\n/, $_, 5;
				while ($file[1] =~ /\\/) { $file[1] =~ s/^.*\\//; }
				$file[1] =~ s/"//g;
				chop $file[1];
				$uploadfilename = $file[1];
				chop $file[4]; chop $file[4];
				$uploadfile = $file[4];
			}
			elsif ($_ =~ "name=") {
				($var, $key, $value) = split(/\"/, $_);
				$value =~ s/([\n\f\r])//g;
				$key =~ s/(\.x)|(\.y)//; # Schaltet Koordinaten-Angaben von
Image-Buttons aus
		   		$form{$key} = $value;
			}
		}
	}
}
return 1;

All you need now to use it is the call of the script from you html
page.
the form tag I use looks like that:

<FORM METHOD="POST" ACTION="http://www.your-domain.com/cgi-bin/upload-parser/upload.pl"
ENCTYPE="multipart/form-data">
<!-- For Upload : Methode "POST" und Enctype "multipart/form-data" -->
<FONT face="Arial, Helvetica" size="2" color="#000066">Select
File:</FONT><BR>
<INPUT TYPE="file" NAME="uploaded_file" SIZE=30 MAXLENGTH=80>
<INPUT TYPE="hidden" NAME="hidden" VALUE="versteckt">
<INPUT TYPE="submit" NAME="button" VALUE="upload">
</FORM>

I hope that this solves your problem. The script is by far the best
one I found on the web. If you should have any problems please post a
clarification request before rating my answer.

till-ga

Search Strategy
The original script (with german commenst however) can be downloaded
at:
( http://www.silbersteine.de )

Request for Answer Clarification by horseradish-ga on 31 Oct 2003 05:15 PST
Thanks for this, it looks like just what I need. However, I do have a
couple of questions:

1. I think perhaps you've made a small error as you say:
1st script store as "upload.pl" in your cgi-bin 
and
2nd script store as "upload.pl" in your cgi-bin 
Should the 2nd one be called parser.pl? I have guessed that this is
the case, but when I try to run it, I'm getting a 500 server error.
See it in action so far at http://www.redjam.com/uploadpic.html

2. How would I incorporate it into my existing form, which already has
a <form action etc...> command? I assume I'd put my existing form data
processing script into upload.pl? Would I call my form.cgi from within
upload.pl or would I just have the entire script in full along with
the upload.pl script too? Would it go before or after processing the
image upload info? You can see my existing form at
http://www.redjam.com/testform.html. I'd like to add the image upload
bit to the bottom of it.

Thanks for your help, it looks quite exciting, I hope we can get it to
work!!

Clarification of Answer by till-ga on 31 Oct 2003 05:49 PST
Ups. Sorry for the mistake I made. Your guess is correct of course.
The 2nd script must be stored as parser.pl

Including two form tags in your html file should not be a problem. All
you got to do is explain the function of the second button.
You can place the button anywhere you like to see it on the page.
The processing order is given by the sequence the buttons are pressed.
As the actions are different you must use two form tags.

The other way -which is by far more complicated- is trying to combine
your form.cgi with the routines given in the upload script.
But why ? Those customers who want to upload images won´t see a
problem in sending the text info first and then upload the images.
You could split the whole thing up in two different pages (one for the
text data and one for the image upload) and  make a link to the
"images upload" page.


till-ga

Request for Answer Clarification by horseradish-ga on 05 Nov 2003 14:18 PST
Thanks for your help. I will need to send the pics with the rest of
the info at the same time as it would be messy to submit twice, plus
it would give two results and they may not be identifiable as being a
pair.
Still perhaps I can ask that as another question to someone else.

In the meantime, I still need to get your original code working. I did
rename the files, so I have in my cgi-bin a folder called
'upload-parser' (set to chmod 777), which contains two files:
'parser.pl' and 'upload.pl' (both set to chmod 755). The html browse
page is at http://www.redjam.com/uploadpic.html. If you use it, you'll
see it returns an error 500: internal server error.

Can you help me work out why this is happening please? Thanks very much

Request for Answer Clarification by horseradish-ga on 05 Nov 2003 14:27 PST
Also I have set the folder for the images (www.redjam.com/feverapps)
to chmod 777 as you said.

I see there's a line:
$pathtemplate = "/htdocs/template.htm"; # server path to the template
not necessary to use one

Shall I just take this out, if it's not necessary?

Clarification of Answer by till-ga on 06 Nov 2003 06:49 PST
There´s a little directory error in you form tag.
As you write in your request the perl-scripts are not in the
cgi-bin
directory, but in the
cgi-bin/upload-parser
directory.
So your form tag should be:

<FORM METHOD="POST" ACTION="http://www.redjam.com/cgi-bin/upload-parser/upload.pl"
ENCTYPE="multipart/form-data">

I´m shure it will solve this problem.

Please leave the line
$pathtemplate = "/htdocs/template.htm"; # server path to the template
not necessary to use one
in the code. I´m not really shure what happens if you take the line out. 


till-ga

Request for Answer Clarification by horseradish-ga on 06 Nov 2003 13:41 PST
Sorry, I changed the form tag as I took the files out of that folder
to see if it would make it work. Originally, I had the files in that
folder, with the correct line and it didn't work you see. I'll change
it back now so you can see it still doesn't work even when that line
is as it should.

I tried placing an alert message in the very first part of upload.pl,
before it even gets to the 'require' line and that doesn't even work.
It's as though it can't even get to upload.pl somehow, yet I have
definitely set permissions on it to 755.

Clarification of Answer by till-ga on 06 Nov 2003 23:39 PST
I´m a little bit confused about the problems you have getting the
script running. I got thios script running on several accounts of
customers.

Please try an upload using my server to see that the scripts work:
http://www.stoffel-kueppers.de/formular.htm

Please use the following code for the form then:

<HTML>
<HEAD>
<TITLE>Upload-Parser</TITLE>
</HEAD>
<BODY link="#444444" vlink="#888888" alink="#888888">
<DIV align="center"><FONT face="Arial, Helvetica" size="6"
color="#000066">Upload-Parser
</FONT><br><br>
<FONT face="Arial, Helvetica" size="2" color="#000066">Upload images
</FONT>
  <FORM METHOD="POST" ACTION="http://www.redjam.com/cgi-bin/upload.pl"
ENCTYPE="multipart/form-data"> <!-- Für Upload zwingend: Methode
"POST" und Enctype "multipart/form-data" -->
      <FONT face="Arial, Helvetica" size="2" color="#000066">Select
file:</FONT><BR>
	  <INPUT TYPE="file" NAME="uploaded_file" SIZE=30 MAXLENGTH=80>
      <INPUT TYPE="hidden" NAME="hidden" VALUE="versteckt">
      <INPUT TYPE="submit" NAME="button" VALUE="upload">
  </FORM>
<FONT face="Arial, Helvetica" size="1"><A
href="http://www.silbersteine.de">Upload-Parser &copy; 2001 by
SilberSteine
</A></FONT>	
</BODY>
</HTML>

If it´s still not working then I will write to the editors of Google
Answers and ask them for permission to contact you by mail as it could
be necessary to send you the original completely unmodified files.

We WILL get this to work !

till-ga

Clarification of Answer by till-ga on 07 Nov 2003 00:35 PST
Please have a look at
http://sourceforge.net/projects/upload-parser/
The authors of the scripts have released a new version under the GNU
general public license.
Obviously they have chamned some features, one of them being the
intergration of the upload abilities into existing perl scripts. That
could be exactly what you´ve been looking for.
And there will be support in english at sourceforge.net. 

I hope this helps to solve the problem finally.


till-ga

Request for Answer Clarification by horseradish-ga on 10 Nov 2003 09:11 PST
I'm sorry but this just isn't working. I think the problem is that
you're advising me to use someone else's script but without really
understanding how it works yourself. I do appreciate that you have
tried to help me but I still do not have the working result that I
needed.
I'm now looking at other options for this.

Thank you very much anyway.

Clarification of Answer by till-ga on 10 Nov 2003 10:05 PST
I am sorry that you are not satisfied with the solution I gave.
Did you try the installed script on my domain to see that the scripts
work ? Did you have a look at the new features in the updated version
of the script ?

till-ga
Reason this answer was rejected by horseradish-ga:
Although this researcher did try to help me, it soon became clear that
they had simply given me an off-the-shelf solution and that perhaps
they did not really know very much about Perl scripting themselves.

The researcher had found a script online that they told me would do
the job I needed. I could have found this script myself as I had
browsed several similar scripts before trying Google Answers. Still I
thought I'd give it a try as I assumed the researcher knew the script
very well and would be able to assist me to ensure it did what I
wanted it to, as asked in my original question.

But when it didn't work, the researcher was only able to offer obvious
solutions that even I knew had nothing to do with the problem. I
assumed the reseracher would have a good knowledge of Perl scripting,
to be able to answer my question, but judging by the many
clarification requests I don't think they did.

To complicate matters further, the off-the-shelf script was annotated
in German which wasn't much help!

I do believe the researcher wanted to help me, but sadly wasn't able.
horseradish-ga rated this answer:2 out of 5 stars
I believe this researcher was really trying to help but just didn't
have the Perl scripting knowledge I needed, sorry!

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy