Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loading .PDFs

Status
Not open for further replies.

LAM986

Programmer
Nov 4, 2009
15
US
Hello,
Not sure if this is the correct forum for this question. Through my website, I need to allow a user to enter a number and open a corresponding .pdf. The .pdf name looks like 'TR-0433 72689 to 72864' and the user can enter a number anywhere between 72689 amd 72864. Can anyone direct me to a best method. I'm using Dreamweaver to create the website. Thank you in advance.
 
Hi
[ul]
[li]Which programming languages are supported on your server ?[/li]
[li]Which programming languages you know ?[/li]
[/ul]
For example this could be one way of solving it with a [tt]bash[/tt] CGI script :
Code:
[b]<form[/b] [maroon]action[/maroon][teal]=[/teal][green][i]"/cgi-bin/givemefile.sh"[/i][/green][b]>[/b]
File number : [b]<input[/b] [maroon]type[/maroon][teal]=[/teal][green][i]"text"[/i][/green] [maroon]name[/maroon][teal]=[/teal][green][i]"number"[/i][/green][b]>[/b]
[b]<input[/b] [maroon]type[/maroon][teal]=[/teal][green][i]"submit"[/i][/green] [maroon]value[/maroon][teal]=[/teal][green][i]"Give me that file"[/i][/green][b]>[/b]
[b]</form>[/b]
Code:
[gray]#!/bin/bash[/gray]

[navy]minnumber[/navy][teal]=[/teal][green][i]'72689'[/i][/green]
[navy]maxnumber[/navy][teal]=[/teal][green][i]'72864'[/i][/green]
[navy]pdfdirectory[/navy][teal]=[/teal][green][i]'.'[/i][/green]
[navy]fileprefix[/navy][teal]=[/teal][green][i]'TR-0433 '[/i][/green]
[navy]filesuffix[/navy][teal]=[/teal][green][i]'.pdf'[/i][/green]

[teal][[[/teal] [green][i]"$QUERY_STRING"[/i][/green] [teal]=~[/teal] [navy]number[/navy][teal]=([[:[/teal]digit[teal]:]][/teal]{[purple]5[/purple]}[teal])[/teal] [teal]]][/teal] [teal]&&[/teal] [navy]number[/navy][teal]=[/teal][green][i]"${BASH_REMATCH[1]}"[/i][/green] [teal]||[/teal] [navy]number[/navy][teal]=[/teal][green][i]''[/i][/green]

[navy]name[/navy][teal]=[/teal][green][i]"$pdfdirectory/$fileprefix$number$filesuffix"[/i][/green]

[teal][[[/teal] [teal]![/teal] [green][i]"$number"[/i][/green] [teal]||[/teal] [green][i]"$number"[/i][/green] -lt [green][i]"$minnumber"[/i][/green] [teal]||[/teal] [green][i]"$number"[/i][/green] -gt [green][i]"$maxnumber"[/i][/green] [teal]||[/teal] [teal]![/teal] -f [green][i]"$name"[/i][/green] [teal]]][/teal] [teal]&&[/teal] {
  echo [green][i]$'Content-type: text/plain[/i][/green][lime][i]\r\n\r[/i][/lime][green][i]'[/i][/green]
  echo [green][i]"Invalid file number requested"[/i][/green]
  [COLOR=chocolate]exit[/color]
}

echo -e [green][i]"Content-type: application/octet-stream[/i][/green][lime][i]\r[/i][/lime]
[green][i]Content-disposition: attachment; filename=[/i][/green][lime][i]\"[/i][/lime][green][i]$name[/i][/green][lime][i]\"\r[/i][/lime]
[lime][i]\r[/i][/lime][green][i]"[/i][/green]

cat [green][i]"$name"[/i][/green]
Requires [tt]bash[/tt] version 4.0.0 or newer. Tested on Linux with Apache web server.


Feherke.
 
CGI is definitely supported and .Net might also be available. I'm new to web development. Thank you for your help.
 
Hi

Feherke said:
Which programming languages are supported on your server ?
LAM986 said:
CGI is definitely supported
Note that CGI is not a language.
Common Gateway Interface said:
The Common Gateway Interface (CGI) is a standard protocol that defines how webserver software can delegate the generation of [link ]webpages[/url] to a console application. Such applications are known as CGI scripts; they can be written in any programming language, although scripting languages are often used.
( See the whole Common Gateway Interface article on Wikipedia. )

So my question is not answered yet.

Feherke.
 
Okay, thanks for the CGI information. I have a better understanding. The languages the host supports is PHP and Perl. They have Linux servers. Does this help?
 
Hi

LAM986 said:
The languages the host supports is PHP and Perl.
[ul]
[li]Hosts usually not allow shell scripts as CGI, for security reasons. So probably you can not use my previous code.[/li]
[li]If you decide to use PHP and you have further questions, please post them in forum434.[/li]
[li]If you decide to use Perl, we can continue in this forum, or in forum219.[/li]
[/ul]
To help your decision, here are the PHP and Perl rewrites of the Bash script :
Code:
[teal]<?php[/teal]

[navy]$minnumber[/navy][teal]=[/teal][purple]72689[/purple][teal];[/teal]
[navy]$maxnumber[/navy][teal]=[/teal][purple]72864[/purple][teal];[/teal]
[navy]$pdfdirectory[/navy][teal]=[/teal][green][i]'.'[/i][/green][teal];[/teal]
[navy]$fileprefix[/navy][teal]=[/teal][green][i]'TR-0433 '[/i][/green][teal];[/teal]
[navy]$filesuffix[/navy][teal]=[/teal][green][i]'.pdf'[/i][/green][teal];[/teal]

[b]if[/b] [teal]([/teal][COLOR=darkgoldenrod]is_numeric[/color][teal]([/teal][navy]$_GET[/navy][teal][[/teal][green][i]'number'[/i][/green][teal]]))[/teal] [navy]$number[/navy][teal]=[/teal][navy]$_GET[/navy][teal][[/teal][green][i]'number'[/i][/green][teal]];[/teal] [b]else[/b] [navy]$number[/navy][teal]=[/teal][green][i]''[/i][/green][teal];[/teal]

[navy]$name[/navy][teal]=[/teal][green][i]"$pdfdirectory/$fileprefix$number$filesuffix"[/i][/green][teal];[/teal]

[b]if[/b] [teal](![/teal] [navy]$number[/navy] [teal]||[/teal] [navy]$number[/navy][teal]<[/teal][navy]$minnumber[/navy] [teal]||[/teal] [navy]$number[/navy][teal]>[/teal][navy]$maxnumber[/navy] [teal]||[/teal] [teal]![/teal] [COLOR=darkgoldenrod]file_exists[/color][teal]([/teal][navy]$name[/navy][teal]))[/teal] [teal]{[/teal]
  [COLOR=darkgoldenrod]header[/color][teal]([/teal][green][i]'Content-type: text/plain'[/i][/green][teal]);[/teal]
  [b]echo[/b] [green][i]'Invalid file number requested'[/i][/green][teal];[/teal]
  [b]exit[/b][teal];[/teal]
[teal]}[/teal]

[COLOR=darkgoldenrod]header[/color][teal]([/teal][green][i]'Content-type: application/octet-stream'[/i][/green][teal]);[/teal]
[COLOR=darkgoldenrod]header[/color][teal]([/teal][green][i]"Content-disposition: attachment; filename=\"$fileprefix$number$filesuffix\""[/i][/green][teal]);[/teal]

[COLOR=darkgoldenrod]readfile[/color][teal]([/teal][navy]$name[/navy][teal]);[/teal]
Code:
[gray]#!/usr/bin/perl -w[/gray]

[navy]$minnumber[/navy][teal]=[/teal][purple]72689[/purple][teal];[/teal]
[navy]$maxnumber[/navy][teal]=[/teal][purple]72864[/purple][teal];[/teal]
[navy]$pdfdirectory[/navy][teal]=[/teal][green][i]'.'[/i][/green][teal];[/teal]
[navy]$fileprefix[/navy][teal]=[/teal][green][i]'TR-0433 '[/i][/green][teal];[/teal]
[navy]$filesuffix[/navy][teal]=[/teal][green][i]'.pdf'[/i][/green][teal];[/teal]

[b]if[/b] [teal]([/teal][navy]$ENV[/navy][teal]{[/teal][green][i]'QUERY_STRING'[/i][/green][teal]}[/teal][teal]=~[/teal][green][i]/number=([[:digit:]]{5})/[/i][/green][teal])[/teal] [teal]{[/teal] [navy]$number[/navy][teal]=[/teal][navy]$1[/navy] [teal]}[/teal] [b]else[/b] [teal]{[/teal] [navy]$number[/navy][teal]=[/teal][green][i]''[/i][/green] [teal]}[/teal]

[navy]$name[/navy][teal]=[/teal][green][i]"$pdfdirectory/$fileprefix$number$filesuffix"[/i][/green][teal];[/teal]

[b]if[/b] [teal](![/teal] [navy]$number[/navy] [teal]||[/teal] [navy]$number[/navy][teal]<[/teal][navy]$minnumber[/navy] [teal]||[/teal] [navy]$number[/navy][teal]>[/teal][navy]$maxnumber[/navy] [teal]||[/teal] [teal]![/teal] [teal]-[/teal]f [navy]$name[/navy][teal])[/teal] [teal]{[/teal]
  [b]print[/b] [green][i]"Content-type: text/plain\r\n\r\n"[/i][/green][teal];[/teal]
  [b]print[/b] [green][i]'Invalid file number requested'[/i][/green][teal];[/teal]
  [b]exit[/b][teal];[/teal]
[teal]}[/teal]

[b]print[/b] [green][i]"Content-type: application/octet-stream\r
Content-disposition: attachment; filename=\"[navy]$fileprefix$number$filesuffix[/navy]\"\r
\r\n"[/i][/green][teal];[/teal]

[b]open[/b] FIL[teal],[/teal][green][i]"<$name"[/i][/green][teal];[/teal]
[b]print[/b] [b]while[/b] [teal]([/teal][green][i]<FIL>[/i][/green][teal]);[/teal]
[b]close[/b] FIL[teal];[/teal]
Note that I do not really like the [tt]CGI[/tt] Perl module, so I avoid it for simple tasks like this. In forum219 you will be firmly guided towards the use of [tt]CGI[/tt] module. If you decide to use Perl, make sure you know their points of view too.

Feherke.
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top