By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,021 Members | 1,321 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,021 IT Pros & Developers. It's quick & easy.

Splitting uploads up/ concatenating them back together

P: n/a
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_file 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?

Sep 24 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
nw*******@gmail.com wrote:
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_file function.


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

Cheers,
Nicholas Sherlock
Sep 24 '05 #2

P: n/a
On 23 Sep 2005 21:51:02 -0700, nw*******@gmail.com wrote:
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_file function.


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 :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Sep 24 '05 #3

P: n/a
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?

Sep 24 '05 #4

P: n/a
On Sat, 24 Sep 2005 08:02:34 -0700, nw*******@gmail.com wrote:
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?


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

Sep 24 '05 #5

P: n/a
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_contents($handle);
fclose(handle);

// Get file name

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

// Get size of file

while ($nomore = 0):
if (file_exists($file.'/'.$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($file.'/'.$count)):
$handle = fopen($file.'/'.$count, "rb");
$content = file_get_contents($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_contents() 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_contents() 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?

Sep 25 '05 #6

P: n/a
nw*******@gmail.com wrote:
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_contents($handle);
fclose(handle);

// Get file name

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

// Get size of file

while ($nomore = 0):
if (file_exists($file.'/'.$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($file.'/'.$count)):
$handle = fopen($file.'/'.$count, "rb");
$content = file_get_contents($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_contents() 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_contents() 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?


You're using the file_get_contents function incorrectly.

The following line:
$contents = file_get_contents($filename);

Is equivalent to the following lines:
$handle = fopen($filename,"r");
$contents = fread($handle,filesize($filename));
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
- ki******@NOSPAMbetadome.com
(remove NOSPAM to contact me directly)
Sep 25 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.