Автор Гілка: Завантаження файла на сервер (CGI::Perl)  (Прочитано 2299 раз)

Відсутній Rainbow

  • Новачок
  • *
  • дописів: 45
  • Карма: +0/-0
  • Що новенького?
Є такий фрагмент коду, написаного на перлі:
   my $upload = CGI::Upload->new;
    my $q = $upload->query;
    print "$upload, $q";
    my $file_name = $upload->file_name('file1'); #тут скрит вивалюється

Хто знає реально особливості прийому файлів від клієнта на CGI.
PS. Любителям посилати в ґуґл, рекомендую утриматись. Проходили це.

Відсутній Володимир Лісівка

  • Адміністратор ЩОДО
  • Видавець
  • *****
  • дописів: 3749
  • Карма: +9/-0
  • Програміст
Re: Завантаження файла на сервер (CGI::Perl)
« Відповідей #1 : 2006-01-24 15:10:57 »
Я не зміг знайти функцію file_name в "man CGI".
Замініть її на param('file1').

Дока:

 
Цитата
When the form is processed, you can retrieve the entered filename by calling param():

              $filename = param(’uploaded_file’);

       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 cre-
       ates during upload spooling (see below).

       The filename returned is also a file handle.  You can read the contents of the file using standard Perl file reading
       calls:

               # Read a text file and print it out
               while (<$filename>) {
                  print;
               }

               # Copy a binary file to somewhere safe
               open (OUTFILE,">>/usr/local/web/users/feedback");
               while ($bytesread=read($filename,$buffer,1024)) {
                  print OUTFILE $buffer;
               }

       However, there are problems with the dual nature of the upload fields.  If you "use strict", then Perl will complain
       when you try to use a string as a filehandle.  You can get around this by placing the file reading code in a block con-
       taining the "no strict" pragma.  More seriously, it is possible for the remote user to type garbage into the upload
       field, in which case what you get from param() is not a filehandle at all, but a string.

       To be safe, use the upload() function (new in version 2.47).  When called with the name of an upload field, upload()
       returns a filehandle, or undef if the parameter is not a valid filehandle.

            $fh = upload(’uploaded_file’);
            while (<$fh>) {
                  print;
            }

       In an list context, upload() will return an array of filehandles.  This makes it possible to create forms that use the
       same name for multiple upload fields.

       This is the recommended idiom.
Ну і так далі.
[Fedora Linux]

Відсутній Rainbow

  • Новачок
  • *
  • дописів: 45
  • Карма: +0/-0
  • Що новенького?
Re: Завантаження файла на сервер (CGI::Perl)
« Відповідей #2 : 2006-01-24 15:27:15 »
Пробував так.
Імя файла отримую без проблем. А вот дальше як отримати доступ до файла. Ч-з open не проходить. Дані то в памяті сидять.

Щож до функції file_name її вісдсутність пояснюється відсутністю у вас модуля CGI::Upload

Відсутній Володимир Лісівка

  • Адміністратор ЩОДО
  • Видавець
  • *****
  • дописів: 3749
  • Карма: +9/-0
  • Програміст
Re: Завантаження файла на сервер (CGI::Perl)
« Відповідей #3 : 2006-01-25 20:40:08 »
Спеціально для вас процитую шматок моєї попередньої цитати:  ;)

Цитата
The filename returned is also a file handle. You can read the contents of the file using standard Perl file reading
calls:

# Read a text file and print it out
while (<$filename>) {
print;
}

Це неочевидно - я знаю.
[Fedora Linux]

Відсутній Rainbow

  • Новачок
  • *
  • дописів: 45
  • Карма: +0/-0
  • Що новенького?
Re: Завантаження файла на сервер (CGI::Perl)
« Відповідей #4 : 2006-02-02 16:21:12 »
Спеціально для вас процитую шматок моєї попередньої цитати:  ;)

Цитата
The filename returned is also a file handle. You can read the contents of the file using standard Perl file reading
calls:

# Read a text file and print it out
while (<$filename>) {
print;
}

Це неочевидно - я знаю.
Власне таким макаром рішив цю справу. Потім переглядав тексти CGI::Upload. Ніяк неможу зрозуміти чому перший код не працює...