PDA

Click to See Complete Forum and Search --> : Help with CGI


mendhak
Jul 8th, 2003, 03:34 AM
Let's say I have a link to a URL like this:

domainname.com/cgi-bin/photo.cgi?photo=9319.jpg

Now, the CGI code should take the query string, look at "photo" which in this case is 9319.jpg, and should show the image 9319.jpg on the page.

I don't know any CGI, so I'd really appreciate some code.

Thanks in advance.

mendhak
Jul 9th, 2003, 12:35 AM
If nobody knows CGI, at least a link?

Memnoch1207
Jul 9th, 2003, 12:45 AM
Here (http://www.cgi101.com/class/) ya go froggy.

CornedBee
Jul 9th, 2003, 01:58 AM
What kind of CGI?

I think the query string is the QUERYSTRING or QUERY or QUERY_STRING environment variable. Parse it in your favourite language. Then do
print("Content-type: image/jpeg\n\n");
or the equivalent.

Finally, load the image and write it to stdout.



Alternatively, do a HTTP redirect: instead of the Content-type, write a Location header:
print("Location: parsedfilename.jpg");

mendhak
Jul 10th, 2003, 06:41 AM
Originally posted by CornedBee
I think the query string is the QUERYSTRING or QUERY or QUERY_STRING environment variable. Parse it in your favourite language. Then do
print("Content-type: image/jpeg\n\n");
or the equivalent.

Finally, load the image and write it to stdout.




Thanks for the link Memnoch. I obviously haven't gone through it all, since I"m looking for a very rough solution.

Can you give me an example of the usage of the stdout?

What I'm trying to do is this:

if I put

<img src="http://something.netfirms.com/cgi-bin/pic.cgi?hello.jpg">

it should output the hello.jpg image, no html whatsoever.

CornedBee
Jul 10th, 2003, 07:49 AM
It's been a while since I've done Perl, so here's a C++ CGI:

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

int main()
{
string query(getenv("QUERY"));
query = query.substr(query.find('?')+1);
cout << "Location: " << query << "\r\n" << flush;
}

mendhak
Jul 10th, 2003, 01:44 PM
While you were gone, I cheated on you and did a frantic search on google. I ended up with this code:


$picname = "$ENV{'QUERY_STRING'}";

$MY_FILE_NAME = $picname;
$CHUNK_SIZE = 4096;

open( MY_FILE, "<$MY_FILE_NAME" )
or die( "Can't open $MY_FILE_NAME: $!\n" );

print "Content-type: image/jpeg\r\n";
print "\r\n";

binmode( MY_FILE ); # These are crucial!
binmode( STDOUT );

while ( $cb = read( MY_FILE, $data, $CHUNK_SIZE ) ) {
print $data;
}

close( MY_FILE );



Is it OK as well? How come yours is different from mine? And why does yours look so much like C++?

mendhak
Jul 10th, 2003, 01:46 PM
so here's a C++ CGI


I didn't see that. :p

Anyways, is my code OK as well? Does it make a difference if I do it in Perl or Cpp?

Why does CGI suck so much?

CornedBee
Jul 11th, 2003, 01:26 AM
The C++ is more lightweight, the server doesn't have to start the full Perl interpreter for it.

Still, the overhead of starting a process is always there, which is why PHP or mod_perl are better.


The difference between the scripts:
Mine tells the browser where to look for the real image (HTTP redirect, Location: header).
Yours IS in effect the image.

mendhak
Jul 11th, 2003, 02:12 AM
Alright, thanks a bunch. Though crude, problem solved!