views:

40

answers:

1

I'm using json to open the user popup. I used to use basename( $_FILES['userfile']['name'] ) on php, how to do that on perl?

Server side code:

#!/usr/bin/perl
use CGI;

print "Content-type: text/html; 
Cache-Control: no-cache;
charset=utf-8\n\n";

@allowedExtensions =("jpg","tiff","gif","eps","jpeg","png");

my $q = CGI->new();

my $filename = $q->upload('userfile');

print "file name is $file_name";

Client side code:

var post_obj = new Object();

new AjaxUpload('upload_attachment_button', {
    action: 'upload.cgi',
    type: "POST",
    data: post_obj,

    onChange: function() {},
    onSubmit: function() {
      $("#upload_attachment_button").addClass('ui-state-disabled');
      $("#upload_proj_message").html('<span> class="loading">uploading...</span>');
    },
    onComplete: function(file, response) {
      $("#upload_attachment_button").removeClass('ui-state-disabled');
      alert(response);
    }
});
+1  A: 

Looks like you trying to get name of the file uploaded by user. If you are using CGI module, then here is solution:

use CGI;
my $q = CGI->new();

my $filename = $q->param('userfile'); ## retrive file name of uploaded file

From the manual:

Different browsers will return slightly different things for the name. Some browsers return the filename only. Others return the full path to the file, using the path conventions of the user's machine. Regardless, the name returned is always the name of the file on the user's machine, and is unrelated to the name of the temporary file that CGI.pm creates during upload spooling (see below).

Update:

Sorry, didn't noticed it before. Please add use strict; at the begining of the script. It'll force you to declare all variables. You'll see mistype in print statement:

print "file name is $filename"; ## must be $filename

To declare @allowedExtensions just add my before first use:

my @allowedExtensions =("jpg","tiff","gif","eps","jpeg","png");

Also I believe that you don't need ; at the end of lines when you print HTTP headers:

print "Content-type: text/html 
Cache-Control: no-cache
charset=utf-8\n\n";

And please always use strict. It'll save you tons of time in future.

Ivan Nevostruev
I tried it and it did not work. The file name is returned empty. I simply tried to print the file name returned.
zeina
@zeina Can you please show your code?
Ivan Nevostruev
@Ivan: I added the code. Please note that I'm using json on the client side.
zeina
@zeina Please add HTML and Javascript you're using (server side looks good)
Ivan Nevostruev
@Ivan:added client side code.
zeina
Add `use strict;` and you'll see variable name mistype in `print`. See my update.
Ivan Nevostruev
thanks Ivan for your help.
zeina