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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GD::GRAPH open(fh,"image.$format") FAILS :( cannot figure it out

Status
Not open for further replies.

LinuxNoobPhilip

Technical User
Jan 22, 2008
11
US
So what I have done is made a cool .pl script which opens the database takes out some data, uses GD:graph to make a nice line graph and it creates the graph, the line of interest is

open(IMG, ">graph/the_image.$format") or die $1;

this line works when I run the script.

HERE IS WHERE IT BREAKS DOWN.
I took my script and put it into my cgi-bin folder (apache server) and did a 'chmod a+x file.pl'

On my index.php I made a form here is a snipet:
<form action="/cgi-bin/getgraph.pl" method="POST" target = "_top">

Now here is what happens, when I test this through my webserver it links to the perl script fine. My POST extraction works fine, it starts printing out my outputs. But when it gets to:

open(IMG, ">graph/the_image.$format") or die $1;

IT FAILS :(


my theory: I think that the server doesn't have permission to open files.

p.s. Also I cannot load a picture in that folder into my index.php either.


Help would be much appreciated
 
Cool I changed it to that and it did not fix the problem. That line still fails for some reason when I run it through the form on my index.php

(it works when I sudo run it)

anything else you could think of?
 
that is part of the weirdness here is what I have

#### snipet #####
print "test ";
open(IMG, ">../images/the_image.$format") or die "Can't write file: $!";
print "test ";
#####end snipet ######

The first "test" shows up on the web page but the error from the open doesn't, nor does the second "test".
 
Invocations of die normally go to the error log. Have you checked your webserver error log?

The way around that is to add the following package ot the beginning of your perl cgi script:

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]CGI::Carp[/green] [red]qw([/red][purple]fatalsToBrowser[/purple][red])[/red][red];[/red]
[tt]------------------------------------------------------------
Core (perl 5.10.0) Modules used :
[ul]
[li]CGI::Carp - CGI routines for writing to the HTTPD (or other) error log[/li]
[/ul]
[/tt]

- M
 
Ultimately what you need to do is simply go to the directory you're attempting to write and chmod a+w it.

However, there are a couple things to point out first. You should always give yourself as meaningful of error messages as possible. Currently, you only report the error, but not the subject of the error. One possible problem could actually be that the relative path is not working correctly, so you don't even know where it's trying to write to.

Change your code to the following to clear up what you're attempting to do:

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]File::Spec::Functions[/green] [red]qw([/red][purple]rel2abs catfile[/purple][red])[/red][red];[/red]

[black][b]use[/b][/black] [green]strict[/green][red];[/red]
[black][b]use[/b][/black] [green]warnings[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$image_dir[/blue] = [maroon]rel2abs[/maroon][red]([/red][red]'[/red][purple]../images/[/purple][red]'[/red][red])[/red][red];[/red]

[olive][b]if[/b][/olive] [red]([/red]! [url=http://perldoc.perl.org/functions/-X.html][black][b]-e[/b][/black][/url] [blue]$image_dir[/blue][red])[/red] [red]{[/red]
	[url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Directory does not exist: [blue]$image_dir[/blue][/purple][red]"[/red][red];[/red]
[red]}[/red]

[olive][b]if[/b][/olive] [red]([/red]! [url=http://perldoc.perl.org/functions/-X.html][black][b]-w[/b][/black][/url] [blue]$image_dir[/blue][red])[/red] [red]{[/red]
	[black][b]die[/b][/black] [red]"[/red][purple]Directory is not writable: [blue]$image_dir[/blue][/purple][red]"[/red][red];[/red]
[red]}[/red]

[black][b]my[/b][/black] [blue]$file[/blue] = [maroon]catfile[/maroon][red]([/red][blue]$image_dir[/blue], [red]"[/red][purple]the_image.[blue]$format[/blue][/purple][red]"[/red][red])[/red][red];[/red]

[url=http://perldoc.perl.org/functions/open.html][black][b]open[/b][/black][/url][red]([/red][black][b]my[/b][/black] [blue]$fh[/blue], [red]'[/red][purple]>[/purple][red]'[/red], [blue]$file[/blue][red])[/red] or [black][b]die[/b][/black] [red]"[/red][purple]Can't open [blue]$file[/blue]: [blue]$![/blue][/purple][red]"[/red][red];[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.10.0) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[li]warnings - Perl pragma to control optional warnings[/li]
[/ul]
Core (perl 5.10.0) Modules used :
[ul]
[li]File::Spec::Functions - portably perform operations on file names[/li]
[/ul]
[/tt]

- Miller
 
Use the full path to the file

">the/full/path/to/images/the_image.$format"

or try:

">$ENV{DOCUMENT_ROOT}/images/the_image.$format"

I assume the dot is where the file extension goes and is not supposed to be the concatenation operator.




------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top