1

I have recently started with perl (this week). A client will ask the server for a file, the server has to create the file with the data requested.

So i've been trying to create a file but it is denying permission.

For now: This is the portion of code. I took it from the internet.

  $filename = 'report.txt';
  open($fh, '>', $filename) or $message="Could not open file '$filename' $!";
  print $fh "My first report generated by perl\n";
  close $fh;

I will continue with the data i have to write, after i can make this work.

I am using lighttpd with "www-data" user. I've been several hours trying to solve it... Moreover, i am using $message because i don't know how to use "die". When i used "die" it would show "500 - Internal Server Error" and nothing else.

I am working in Ubuntu 14.04

Cholax
  • 48
  • 1
  • 6
  • Does the user that runs the script have write permission in that directory? And, in case that file already exists, does the user have write permissions on that file as well?. For example, if you login as that user and `cd` to that directory, does it work if you run `touch report.txt`? – Fabio says Reinstate Monica May 26 '17 at 21:36
  • 1
    You should include `use strict;` and `use warnings;` at the beginning of all the code you write. – bytepusher May 26 '17 at 21:41

2 Answers2

5

As to handling a 500 Server Error, see the answers to this question. Basically, find the server's error log.

Alternatively, you can display errors to the browser. This should be off by default and only turned on in development. There's various modules to do this for you, it depends on what web framework you're using. If you're using something barebones like CGI, then it's CGI::Carp. If you're using something like Dancer, which you probably should, then Dancer::Logger controls where the error messages go.


You're getting a permissions error because, surprise, the user running your code does not have permission to write a file. In this case it's the www-data user. I can't tell you why you're being denied permission, you'll have to figure that out yourself. It's probable the directory is not allowing www-data to write files. The permissions on www-data are generally very restricted because web servers are an easy target for attacks.

This tutorial on Unix file permissions should help.

If you're using Dancer you can run the program from the command line and get errors right there on console. You'd have to sudo -u www-data to ensure you're running it as www-data, running it as your own user will not simulate www-data's permission issues.

Schwern
  • 153,029
  • 25
  • 195
  • 336
1

Its probably your user doesn't have access to the folder you're trying to write to.

Imagine that you drop a perl script to your directory, then execute it with a second request...

Try creating the file outside your document root

This will send the error messages to the browser

use CGI::Carp qw(fatalsToBrowser);
die "Bad error here";

But you may need to install the module

Don't forget to use strict; in perl

KeepCalmAndCarryOn
  • 8,817
  • 2
  • 32
  • 47