I'm confused as to where the file for an uploaded file is written to, the OPEN function always fails on me. I always get file permission problem or file or directory does not exist. I'm using Mac OS X's built in Apache Server. I'm unclear as to the structure of where files will be uploaded and how to write it to a directory such as the directory is located in /Library/WebServer/Documents/fileuploads/
#!/usr/bin/perl use CGI; $q = new CGI; print $q->header, $q->start_html(-title=>"Upload Results",-bgcolor=>"w +hite"); print $q->h2("Upload Results"); $file = $q->param("upfile"); if (!$file) { print "Nothing uploaded?<p>\n"; } else { print "Filename: $file<br>\n"; $ctype = $q->uploadInfo($file)->{'Content-Type'}; print "MIME Type: $ctype<br>\n"; #where is ">$file" going to be written to? and how to set to differen +t directory? open(OUT,">$file") or dienice("Can't open outfile for writing: $!" +); $flen = 0; while (read($file,$i,1024)) { print OUT $i; $flen = $flen + 1024; if ($flen > 5120000) { close(OUT); dienice("Error - file is too large. Save aborted.<p>"); } } close(OUT); print "Length: ",$flen/1024,"Kb<p>\n"; print "File saved!<p>\n"; # display the image. this only works because we have a symlink from # tmp.gif/jpeg in the current directory, to /tmp/outfile. if ($ctype eq "image/gif") { print "Here's what you sent:<p>\n"; print "<img src=\"tmp.gif\" border=1><p>\n"; } elsif ($ctype eq "image/jpeg") { print "Here's what you sent:<p>\n"; print "<img src=\"tmp.jpg\" border=1><p>\n"; } } $q->end_html; sub dienice { my($msg) = @_; print "<h2>Error</h2>\n"; print "$msg<p>\n"; exit; }

In reply to CGI to upload file can't access filehandle by macaroni

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.