Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Cgi-bin : printing .txt files

by teddyttas (Novice)
on Dec 18, 2012 at 15:59 UTC ( #1009417=perlquestion: print w/ replies, xml ) Need Help??
teddyttas has asked for the wisdom of the Perl Monks concerning the following question:

hi you all!

I would like to know something. I have a script running with cgi::application in a server. The script is clearly in a cgi-bin.

My script should guide the user to the creation of some modules and definition of parameters. I want to make possible for the user to save the commands and the script on a txt file, to visualize it and then to downloading.

The question comes here: I created a tmp directory into the cgi-bin. I would like to create it out of it, is it possible?

then, how can I print the .txt file in the browser? what's the best choice?

Thank you for your answer, enlighted masters!

Comment on Cgi-bin : printing .txt files
Re: Cgi-bin : printing .txt files
by kennethk (Monsignor) on Dec 18, 2012 at 16:11 UTC

    I'm having a little difficulty following your question. See How do I post a question effectively?. If my comments below are off-point, let me know and we can try to approach a solution.

    To create a temporary directory, I use tempdir from File::Temp. It handles issues of uniqueness and can clean up when you are done, which is incredibly helpful for web projects.

    Is there a reason you need to literally create a text file rather than just returning text to the browser, which is the usual approach? If you want to do things like trigger a 'save to desktop' type response, you can use the MIME header Content-Disposition to specify intent, though what action the user takes is up to them. I guess I'm just unclear what you expect the user to do that is different between visualizing and downloading your document.


    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      Sorry, I've been doing some other projects and I've a little forgot this subject for a while.

      what I would like to add to my site is the possibility to save projects in a file in order to let the user download a zip (I'm watching the package Archive::Zip in order to do this), when they are done (My site is an attempt to make object oriented programmation easier. The result of the user's work on the site should be a number of pm files to download)

      I can create a temporary directory on the server, but I only get to create it on the cgi-bin dir.

      I would like to create it on the public html dir, cause in cgi-bin, as a txt file is a non-executable file, it doesn't let me show the content on the browser. I would like to be able to do that to so that the user could check the files he is creating while working.

      When I try to do this locally, there is no problem, but on the server I can't acces the cgi-bin's upper folder and create there my temporary dir to store the txt files.

      It's the first time I try to run a script on a server, so please, be patient :)
      When I'll be done with this project I hope I'll be able to return something useful to the community.

      P.S. : here is the part of the script that creates me the tmp dir on the cgi-bin :

      my $dirname ="tmp/$utilisateur"; mkdir("$dirname"); open FILE, ">", "$dirname/file.txt" or die $!; foreach my $key ($q->param()) { print FILE $key . " " . $q->param($key) . "\n"; } close FILE; return $self->redirect("http://perlmonks.org");
      I tried to modify the dirname variable in many ways, starting from the classic :
      my $dirname ="../tmp/$utilisateur";
      but I wasn't able to make it work

        To quote Archive::Zip,

        One of the most common ways to use Archive::Zip is to generate Zip files in-memory
        I generally find dealing with tmp files in a web context is messier than doing this all in memory, but that's my two cents and not the question you asked.

        The directory creation problem is likely an issue with file permissions. From a web security perspective, I think that limitation makes a lot of sense, but if your server admin is comfortable with what you want to do, you should be able to accomplish this by modding whatever use your web server runs with (apache, perhaps) to have write privileges in /var/www/html or whatever yours is.

        Rather than taking your approach, and assuming you are committed to doing this via redirect instead of just simple streaming, I would create the files in question in the /tmp directory and then provide a link to a script whose whole function is opening the file (name passed in the query string). Again, this has web security problems, since a naive implementation gives a bad person access to read your entire file system. Note you will have to also implement some kind of cache cleaning behavior in a separate script, probably via cron job.


        #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1009417]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2014-12-19 23:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (94 votes), past polls