Splitting uploads up/ concatenating them back together

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • nwheavyw8@gmail.com

    Splitting uploads up/ concatenating them back together

    I am currently trying to write a simple PHP script that will split an
    uploading file up into 500kb "chunks", then read and concatenate them
    back together when accessed for download. I can't seem to be able to
    find a way to split the file purely in PHP while it is in the middle of
    uploading using the move_uploaded_f ile function.
    I am trying to get it to store the file like so:

    MySong.mp3 3mb
    gets uploaded to my server in this format:

    /uploads/MySong/1 500kb
    /uploads/MySong/2 500kb
    /uploads/MySong/3 500kb
    /uploads/MySong/4 500kb
    /uploads/MySong/5 500kb
    /uploads/MySong/6 500kb

    Each of the "numbers" are the chunks that have been split up

    Then the script to download reads these chunks and ECHOS them back out
    to the as a single file (possibly even forcing a download by setting
    the correct MIME type)

    Is this possible at all with just PHP scripts?

  • Nicholas Sherlock

    #2
    Re: Splitting uploads up/ concatenating them back together

    nwheavyw8@gmail .com wrote:[color=blue]
    > I am currently trying to write a simple PHP script that will split an
    > uploading file up into 500kb "chunks", then read and concatenate them
    > back together when accessed for download. I can't seem to be able to
    > find a way to split the file purely in PHP while it is in the middle of
    > uploading using the move_uploaded_f ile function.[/color]

    The client would have to know how to send the file in chunks. AFAIK, no
    browser can do it.

    Cheers,
    Nicholas Sherlock

    Comment

    • Andy Hassall

      #3
      Re: Splitting uploads up/ concatenating them back together

      On 23 Sep 2005 21:51:02 -0700, nwheavyw8@gmail .com wrote:
      [color=blue]
      >I can't seem to be able to
      >find a way to split the file purely in PHP while it is in the middle of
      >uploading using the move_uploaded_f ile function.[/color]

      You can't; PHP doesn't start running code until the file upload has finished.

      I recall there being a patch to support upload progress displays (through
      partial output and buffering and/or Javascript probably) but IIRC these
      required a patch to PHP itself to modify how it receives file uploads.

      Splitting it server-side is easy enough but doesn't sound it's what you're
      after. Splitting it client-side with just (portable) Javascript doesn't sound
      possible given how restricted it is (and rightly so) in accessing the user's
      machine.
      --
      Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
      http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

      Comment

      • nwheavyw8@gmail.com

        #4
        Re: Splitting uploads up/ concatenating them back together

        okay, I think that I am just going to have to write a program to split
        it client-side then send them over. I know I can do that, but how would
        I go about concatenating them back together? Would I use a loop to
        read and echo one file at a time or something?

        Comment

        • Lāʻie Techie

          #5
          Re: Splitting uploads up/ concatenating them back together

          On Sat, 24 Sep 2005 08:02:34 -0700, nwheavyw8@gmail .com wrote:
          [color=blue]
          > okay, I think that I am just going to have to write a program to split it
          > client-side then send them over. I know I can do that, but how would I go
          > about concatenating them back together? Would I use a loop to read and
          > echo one file at a time or something?[/color]

          The hardest part would be knowing how many parts there are and the
          correct order for them. Either have the user supply a manifest file (list
          of all the files, one per line) or enter how many parts and indicate if
          the suffix is padded (file.mpg.01) or not (file.mpg.1). You now have
          enough information to continue cycling over the upload screen until all
          parts are on the server. At this point, just concat the files together in
          the proper order.

          HTH,
          Laie Techie

          Comment

          • nwheavyw8@gmail.com

            #6
            Re: Splitting uploads up/ concatenating them back together

            okay, my downloader script looks like this:

            <?php

            // Set some values

            $file = &$_GET['file'];
            $nomore = 0;
            $count = 1;
            $size = 0;

            // Get MIME type

            $handle = fopen($file.'/fmime', "r");
            $mime = file_get_conten ts($handle);
            fclose(handle);

            // Get file name

            $handle = fopen($file.'/fname', "r");
            $name = file_get_conten ts($handle);
            fclose(handle);

            // Get size of file

            while ($nomore = 0):
            if (file_exists($f ile.'/'.$count)):
            $size .= $size + filesize($file. '/'.$count);
            else:
            $nomore = 1;
            endif;
            endwhile;


            // Reset values

            $nomore = 0;
            $count = 1;

            // Concatenate file

            header("Content-length: $size"); //$size is the size of $content in
            bytes
            header("Content-type: $mime");
            header("Content-Disposition: attachment; filename=$name" ); //whatever
            name you want to give the file
            while ($nomore = 0):
            if (file_exists($f ile.'/'.$count)):
            $handle = fopen($file.'/'.$count, "rb");
            $content = file_get_conten ts($handle);
            echo $content;
            fclose($handle) ;
            else:
            $nomore = 1;
            endif;
            endwhile;

            and I uploaded a little test file (already splitted and stuff), but
            when I try to download it, i get these errors:


            Warning: file_get_conten ts() expects parameter 1 to be string, resource
            given in /home/freehost/t35.com/l/i/lildude/downloader.php on line 13

            Warning: fclose(): supplied argument is not a valid stream resource in
            /home/freehost/t35.com/l/i/lildude/downloader.php on line 14

            Warning: file_get_conten ts() expects parameter 1 to be string, resource
            given in /home/freehost/t35.com/l/i/lildude/downloader.php on line 19

            Warning: fclose(): supplied argument is not a valid stream resource in
            /home/freehost/t35.com/l/i/lildude/downloader.php on line 20

            Warning: Cannot modify header information - headers already sent by
            (output started at
            /home/freehost/t35.com/l/i/lildude/downloader.php: 13) in
            /home/freehost/t35.com/l/i/lildude/downloader.php on line 40

            Warning: Cannot modify header information - headers already sent by
            (output started at
            /home/freehost/t35.com/l/i/lildude/downloader.php: 13) in
            /home/freehost/t35.com/l/i/lildude/downloader.php on line 41

            Warning: Cannot modify header information - headers already sent by
            (output started at
            /home/freehost/t35.com/l/i/lildude/downloader.php: 13) in
            /home/freehost/t35.com/l/i/lildude/downloader.php on line 42

            what am I doing wrong?

            Comment

            • Kim André Akerø

              #7
              Re: Splitting uploads up/ concatenating them back together

              nwheavyw8@gmail .com wrote:
              [color=blue]
              > okay, my downloader script looks like this:
              >
              > <?php
              >
              > // Set some values
              >
              > $file = &$_GET['file'];
              > $nomore = 0;
              > $count = 1;
              > $size = 0;
              >
              > // Get MIME type
              >
              > $handle = fopen($file.'/fmime', "r");
              > $mime = file_get_conten ts($handle);
              > fclose(handle);
              >
              > // Get file name
              >
              > $handle = fopen($file.'/fname', "r");
              > $name = file_get_conten ts($handle);
              > fclose(handle);
              >
              > // Get size of file
              >
              > while ($nomore = 0):
              > if (file_exists($f ile.'/'.$count)):
              > $size .= $size + filesize($file. '/'.$count);
              > else:
              > $nomore = 1;
              > endif;
              > endwhile;
              >
              >
              > // Reset values
              >
              > $nomore = 0;
              > $count = 1;
              >
              > // Concatenate file
              >
              > header("Content-length: $size"); //$size is the size of $content in
              > bytes
              > header("Content-type: $mime");
              > header("Content-Disposition: attachment; filename=$name" ); //whatever
              > name you want to give the file
              > while ($nomore = 0):
              > if (file_exists($f ile.'/'.$count)):
              > $handle = fopen($file.'/'.$count, "rb");
              > $content = file_get_conten ts($handle);
              > echo $content;
              > fclose($handle) ;
              > else:
              > $nomore = 1;
              > endif;
              > endwhile;
              >
              > and I uploaded a little test file (already splitted and stuff), but
              > when I try to download it, i get these errors:
              >
              >
              > Warning: file_get_conten ts() expects parameter 1 to be string,
              > resource given in /home/freehost/t35.com/l/i/lildude/downloader.php
              > on line 13
              >
              > Warning: fclose(): supplied argument is not a valid stream resource in
              > /home/freehost/t35.com/l/i/lildude/downloader.php on line 14
              >
              > Warning: file_get_conten ts() expects parameter 1 to be string,
              > resource given in /home/freehost/t35.com/l/i/lildude/downloader.php
              > on line 19
              >
              > Warning: fclose(): supplied argument is not a valid stream resource in
              > /home/freehost/t35.com/l/i/lildude/downloader.php on line 20
              >
              > Warning: Cannot modify header information - headers already sent by
              > (output started at
              > /home/freehost/t35.com/l/i/lildude/downloader.php: 13) in
              > /home/freehost/t35.com/l/i/lildude/downloader.php on line 40
              >
              > Warning: Cannot modify header information - headers already sent by
              > (output started at
              > /home/freehost/t35.com/l/i/lildude/downloader.php: 13) in
              > /home/freehost/t35.com/l/i/lildude/downloader.php on line 41
              >
              > Warning: Cannot modify header information - headers already sent by
              > (output started at
              > /home/freehost/t35.com/l/i/lildude/downloader.php: 13) in
              > /home/freehost/t35.com/l/i/lildude/downloader.php on line 42
              >
              > what am I doing wrong?[/color]

              You're using the file_get_conten ts function incorrectly.

              The following line:
              $contents = file_get_conten ts($filename);

              Is equivalent to the following lines:
              $handle = fopen($filename ,"r");
              $contents = fread($handle,f ilesize($filena me));
              fclose($handle) ;

              The "Cannot modify header information" errors are just errors that
              follow due to the fact that the first error already has sent headers
              back to the client in order to be displayed.

              --
              Kim André Akerø
              - kimandre@NOSPAM betadome.com
              (remove NOSPAM to contact me directly)

              Comment

              Working...