Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Uploading Time

by Anonymous Monk
on Jun 13, 2002 at 22:50 UTC ( [id://174366]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Im workin with this
<input type="file" name="file3" size="20">
And what I need to do is just get the file they gave me, not the whole "C:/aplle/index.htm" just "index.htm". how could I do a search and replace or something for this. This is my current code.
for ($i=1; $i<=5; $i++) { if ($q->param("file$i")) { } }

Replies are listed 'Best First'.
Re: Uploading Time
by jarich (Curate) on Jun 14, 2002 at 00:55 UTC
    This has been asked about a lot. One recent discussion is here. The answer is that there are many ways to do it. :)

    I recommend using File::Basename as a portable solution.

    Hope it helps.

    jarich

      I cannot agree more strongly with this approach involving File::Basename suggested by jarich, it being the same which I employed, with HTTP::BrowserDetect for portability, for the file name determination in the CGI::Upload module.

      From the CGI::Upload module ...

      # Determine and set the file system parsing routines # based upon HTTP client header information. # fileparse_set_fstype( sub { my $browser = HTTP::BrowserDetect->new; return 'MSWin32' if $browser->windows; return 'MacOS' if $browser->mac; $^O; } ); my @file = fileparse($cgi->param($param), '\.[^\.]*'); # Return undef if file name cannot be parsed from file # field parameter # return undef unless $file[0];

       

Re: Uploading Time
by SarahM (Monk) on Jun 13, 2002 at 22:57 UTC
    Well, you could try spliting on / and \ and take the last element. It could look like this
    my $path = 'c:\\path\\file'; # The path and file name my $filename = (split (/[\/\\]/, $path))[-1]; # Split on / and \, and +then get the last element
      ok what does \/\\ mean, does mean \ or /, and does -1 mean the last element, what if it was 1.
        /[\/\\]/ is a regex that matches on either '/' or '\'. The first and last '/' starts and ends the regex. The '[' and ']' means match any character between them, instead of a series of charaters. The '\/' mataches '/' and '\\' matches '\', both of them are escaped.

        Split takes the regex and any time it matches, it seperates the string. It then returns a list of values, and the -1 gives you the last item on the list. If you used 1, then you would get the second item on the list (0 is the first element).

Re: Uploading Time (getting last element of a variable)
by thatguy (Parson) on Jun 13, 2002 at 23:27 UTC
    sure it's a cludge and there's a better way, but why split when you can just do:
    $file="C:/aplle/index.htm"; $file=~ s/(.| )*\///; # delete everything(!) upto the last / print $file;

    and to answer your question there AM, you have to escape a / if you use it in a regex because there are /'s as the separator. Naturally if your slash goes the other way (and there is no shame in that) you still have to escape it because perl thinks you are escaping the character after the \.
    -phill

      Since he is working on a file upload to a webserver, the slash could be either way, it depends on how the client sends it. But this should work for either
      $file="C:/aplle/index.htm"; $file=~ s/.*[\/\\]//; # delete everything(!) upto the last / or \ print $file;
      P.S. Why did you use (.| )* instead of .* ?
        no reason other than just not having used said .*

        technically, i should not have used that at all, but I thought I would throw another perspective in. and it does work, just as long as the input data never changes, etc (it's what drives my archaic website)
        -phill

      If one really wants to do it with a regex, a much better way would be
      $fullname = "C:\\apple\\index.htm"; my ($file) = $fullname =~ m!([^/\\]+)$!; print $file;
      Zero backtracking here. However, Macs are still going to be a problem (paths are separated with doublecolons there, right?), so File::Basename is what one should use.
      use File::Basename; # I ain't afraid of no ghosts.. $fullname = "C:\\apple\\index.htm"; my $file = basename $fullname; print $file;
      ____________
      Makeshifts last the longest.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-03-28 22:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found