[Added file upload example. bjorn@bringert.net**20050720175702] { hunk ./cgi.cabal 25 +Executable: upload +Main-Is: upload.hs +Hs-Source-Dir: examples + addfile ./examples/upload.hs hunk ./examples/upload.hs 1 +#!/usr/bin/env runghc + +{-# OPTIONS_GHC -package cgi #-} + +-- | Accepts file uploads and saves the files in the given directory. +-- WARNING: this script is a SECURITY RISK and only for +-- demo purposes. Do not put it on a public web server. +module Main where + +import Control.Monad (liftM) +import Data.Maybe (fromJust) + +import Network.NewCGI + +dir = "../upload" + +upload = + do m <- getInputFilename "file" + case m of + Just n -> saveFile n + Nothing -> printForm + +saveFile n = + do + cont <- liftM fromJust (getInput "file") + let p = dir ++ "/" ++ basename n + liftIO $ writeFile p cont + output $ "Saved as " ++ p ++ "." + +printForm = + output $ "
" + ++ "
" + ++ "" + ++ "
" + +basename = reverse . takeWhile (`notElem` "/\\") . reverse + + +main = runCGI upload }