Results 1 to 13 of 13

Thread: Upload + BZ2 Compression

  1. #1
    Senior Member ajbrun's Avatar
    Join Date
    Apr 2004
    Location
    York, England
    Posts
    4,840
    Thanks
    4
    Thanked
    25 times in 13 posts

    Upload + BZ2 Compression

    I know that you can make a file in PHP that will enable users to upload a file from their hard drive to a web server where the PHP file is located. I'm looking for something that will do this, but then also compress the file into the BZ2 format aswell. It doesn't have to be PHP, but that all I know that does anything similar at the moment.

    Anyone got any ideas?

    Thanks .

  2. #2
    Registered+
    Join Date
    Jul 2006
    Posts
    46
    Thanks
    0
    Thanked
    0 times in 0 posts
    I'm a Perl programmer and could put a quick script together for you if you can get Perl modules installed on the server?

  3. #3
    Senior Member ajbrun's Avatar
    Join Date
    Apr 2004
    Location
    York, England
    Posts
    4,840
    Thanks
    4
    Thanked
    25 times in 13 posts
    Quote Originally Posted by cubabit
    I'm a Perl programmer and could put a quick script together for you if you can get Perl modules installed on the server?
    That should be possible, thanks .

    Could you make it so that it uploads a few files at a time if possible too, where one of the files would be quite large (a few MB at least)?

  4. #4
    Moderator DavidM's Avatar
    Join Date
    Jan 2005
    Posts
    8,779
    Thanks
    800
    Thanked
    252 times in 234 posts
    You may have to check the max file size allowed for uploads as well

  5. #5
    Registered+
    Join Date
    Jul 2006
    Posts
    46
    Thanks
    0
    Thanked
    0 times in 0 posts
    Hi ajbrun,

    I have put a quick script together. I was hoping to use a Bzip2 compression module but had problems with Compress::Bzip2.

    So i simply saved each file and the called bzip2 through the system. You must note that to be safe you should check the filename thoroughly (I put in a test that the filename is in the format 'abc.ext'.

    also note the vars for the location of the files and the redirection URL

    Let me know how you get on and if you need further help!

    Code:
    #!/usr/bin/perl
    
    use CGI;
    use File::Basename;
    use strict;
    
    # set redirect URL here
    my $redirect_url = 'http://redirect-url';
    
    # change directory to repository
    chdir('/path/to/repository/');
    
    # create CGI object
    my $cgi = CGI->new();
    
    foreach my $param ($cgi->param()) {
      if (my $upload = $cgi->upload($param)) {
        # get file name
        my $filename = $cgi->param($param);
    
        if ($filename =~ /\\/) {
          fileparse_set_fstype('MSWin32');
        }
    
        $filename = basename($filename);
        
        # CHECK FILE NAME
        # use regexes to make sure filename won't cause security problems when handed to bzip
        die unless ($filename =~ /^\w+\.\w+$/);
    
        # continue if file exists
        next if (-e $filename . '.bz2');
    
        # save file
        open(FILE, '>', $filename);
        while (my $bytes_read = read($upload, my $buffer, 1024)) {
          print FILE $buffer;
        }
        close(FILE);
    
        # zip
        system("/bin/bzip2 -qz '$filename'");
      }
    }
    
    print $cgi->redirect($redirect_url);
    Last edited by cubabit; 27-07-2006 at 11:24 AM.

  6. #6
    Senior Member ajbrun's Avatar
    Join Date
    Apr 2004
    Location
    York, England
    Posts
    4,840
    Thanks
    4
    Thanked
    25 times in 13 posts
    How do I use it? Do I just put that in a file with a .pl extension and then call it from the web browser once it's been uploaded to the server?

  7. #7
    Registered+
    Join Date
    Jul 2006
    Posts
    46
    Thanks
    0
    Thanked
    0 times in 0 posts

    Lightbulb

    Oops, yes, you need to save it as something like '/cgi-bin/upload.pl' and do something like this in your HTML (don't forget the enctype parameter to the form tag):

    Code:
    <form method="post" action="/cgi-bin/upload.pl" enctype="multipart/form-data">
      <input type="file" name="file_1" /><br />
      <input type="file" name="file_2" /><br />
      <input type="file" name="file_3" /><br />
      <input type="submit" />
    </form>

  8. #8
    Registered+
    Join Date
    Jul 2006
    Posts
    46
    Thanks
    0
    Thanked
    0 times in 0 posts
    And don't forget to give it UNIX line feeds if you're running it on UNIX/Linux etc. Oh, and make it executable:

    Code:
    chmod 755 upload.pl

  9. #9
    Senior Member ajbrun's Avatar
    Join Date
    Apr 2004
    Location
    York, England
    Posts
    4,840
    Thanks
    4
    Thanked
    25 times in 13 posts
    Thanks, I'll probably try and get it sorted tomorrow then .

  10. #10
    Senior Member ajbrun's Avatar
    Join Date
    Apr 2004
    Location
    York, England
    Posts
    4,840
    Thanks
    4
    Thanked
    25 times in 13 posts
    Where should it save the uploaded files?

    I tried uploading 2 fairly small files to test it, but I can't see where it's put them at all.

    If it helps, I didn't use the folder 'cgi-bin', instead I edited the HTML file, and put both the HTML and perl file in the same directory.

    /bin/bzip2 - I don't see that folder directory anywhere for example which seems to be in the code.
    Last edited by ajbrun; 31-07-2006 at 01:14 AM.

  11. #11
    Registered+
    Join Date
    Jul 2006
    Posts
    46
    Thanks
    0
    Thanked
    0 times in 0 posts
    Did it redirect correctly? If so then the perl script is running OK and it being in the same directory should be alright.

    Did you set the 'repostitory' variable? This is where the script will save the files. You need to make sure your webserver has permissions to write to this directory. Thie best way is to do this if you have shell access (and if you are using a UNIX style OS - BTW which OS are you using?):

    Code:
    chown 777 /path/to/repository
    Also you need to change the path to your bzip2 binary. If you have shell access try (again, I;m assuming a UNIX style OS):

    Code:
    whereis bzip2
    If it isn't listed then this isn;t really going to work without bzip2 on your system!

  12. #12
    Senior Member ajbrun's Avatar
    Join Date
    Apr 2004
    Location
    York, England
    Posts
    4,840
    Thanks
    4
    Thanked
    25 times in 13 posts
    Hmmm

    I don't have any kind of SSH access to the server I'm planning on using, and I'm sure I don't have enough access to know where bzip2 is located for example. I'll try another server instead where I have a bit more access.

    Thanks again .

  13. #13
    Registered+
    Join Date
    Jul 2006
    Posts
    46
    Thanks
    0
    Thanked
    0 times in 0 posts
    Creating a small shell script like this and uploading it to your cgi-bin may show you where bzip2 is when you request it from your browser:

    Code:
    #!/bin/sh
    echo Content-type:text/plain
    echo
    whereis bzip2
    make sure you make it executable (chmod 755).

    As for setting the permissions for the repository, you should be able to do that in FTP.

    I do hope you're on a UNIX style OS or none of this will help at all!
    Last edited by cubabit; 02-08-2006 at 04:34 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. 2X eVGA 6800U PCI-E SLI benchmarks
    By eva2000 in forum Graphics Cards
    Replies: 2
    Last Post: 06-03-2005, 05:57 AM
  2. upload site to upload word docs?
    By Chunky in forum General Discussion
    Replies: 11
    Last Post: 14-01-2005, 01:13 AM
  3. The most efficient compression?
    By Steve in forum Software
    Replies: 7
    Last Post: 14-08-2004, 01:41 PM
  4. somewhere to upload images to?
    By micovwar in forum Software
    Replies: 3
    Last Post: 11-10-2003, 03:59 PM
  5. Upload
    By Jcb33 in forum Software
    Replies: 31
    Last Post: 09-09-2003, 01:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •