Jump to content

Upload using chunk_split & base64_encode


mzhou88

Recommended Posts

this is how i splited the uploaded file:

$data = chunk_split(base64_encode(file_get_contents($file)), 9560, "\r\n\r\n\r\nxcowend");
$arrData = explode("\r\n\r\n\r\nxcowend", $data);

- then i counted how manie lines of $arrData is has -1

 

 

 

this is how i joined the splitted parts:

$file = file('test.txt');

$count = count($file)-1;
//echo $count;

$i = 0;
$file = '';
while($i < $count) {
$i = $i+1;
$file .= file_get_contents($i);
}
$filex = base64_decode($file);
$han = fopen('testx.doc','w');
fwrite($han,$filex);
fclose($han);

test.txt has recorded the number of parts the file was splitted into.

 

 

but when the file came out, it was 8kb smaller! and when i open it, it cant be read. weird?

Link to comment
Share on other sites

Replace the entire while loop with this:

 

$file = file_get_contents('test.txt');

 

The while loop was actually putting nonsense into $file, as you were calling file_get_contents() with $i instead of a filename.

Link to comment
Share on other sites

sorry i guess i should have expanded on my first post.

 

currently heres the splitted files in the directory (no extentions):

01

02

03

 

then in test.txt, it is like this:

01

02

03

 

but i just count the lines of in test.txt to check how many parts the file was split up to.

then in the loop, i actually get the contents all into the string $file.

 

if i dunt use loops i dunt know how i could have gotten all the files together.

 

//edit

and if the loop was putting more data into the string, wouldnt the filesize be bigger instead of smaller?

 

thanks! =p

Link to comment
Share on other sites

Ok I get it :)

 

I think a good way to debug will be to store the original base64 encoded string, and the reconstructed string, and print each of them out.  Looking at them you should be able to see what's going wrong.

 

IE, compare $filex in the bottom code to the output of base64_encode(), and see what's changed.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.