473,396 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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_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
6 2254
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: foodic | last post by:
i am fresher to C++ programming, and I just want to learn Concatenating Calls, I have written a program, class SetMe { public: void setX(int x) {_x = x;} void setY(int y) {_y = y;} void...
6
by: Earl Anderson | last post by:
I have a A97/XP applet I've developed for my own use in my department. My boss "suggests" that since I built it, I share it with and instruct the other 6 members of my department on its use. I've...
1
by: ebobnar | last post by:
I need to call the function LoadImage which take a LPCTSTR argument to specify the path to the image to load. However, I need to create this path dynamically from user input by concatenating...
4
by: FB's .NET Dev PC | last post by:
Interesting note, the code below as is will attempt to cast what is clearly indicated as a string into a double. This is becuase the use of + as a concatenation operator. The error message...
0
by: bob brar | last post by:
hi, I have the following function in a class. Public Function getCustomers() As DataSet Dim Db As New CDatabaseAccessor getCustomers = Db.getData("SELECT A.*, RTRIM( A.TITLE)+', '+ RTRIM...
4
by: Richard L Rosenheim | last post by:
Is there any built-in method or mechanism for concatenating two arrays of byte together? I haven't come across anything to do this, and was just checking before I implement some code. Richard...
15
by: Daren | last post by:
Hi, I need to be able to split large string variables into an array of lines, each line can be no longer than 70 chars. The string variables are text, so I would additionally like the lines...
3
by: crater | last post by:
I'm using PHP 5.2. I've created classes to embody HTML elements for a particular project I'm currently working on. All setter methods in the classes return $this so that I can string the...
12
by: lawpoop | last post by:
I'm developing a php website that I have under subversion version control. I'm working on an image upload functionality, and in the middle of it I realized that any files that a user uploads will...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.